Click here to Skip to main content
15,885,244 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,

How do i can use ASP.NET MVC Identity as foreign key ?

I have users using ASP.NET MVC Identity (User individual account),
and they can have many Documents (another model).

so, my users can add documents file.

C#
//*** IdentityUser customized
 public class ApplicationUser : IdentityUser
    {
        public string Email { get; set; }
        public string Country { get; set; }
        public string State { get; set; }
        public string City { get; set; }

        public virtual ICollection<Document> Documents { get; set; }
    }


//*** document category (indiferent for this question)
 public enum DocumentCategory
    {
        Photo = 0,
        Music = 1,
        Video = 2,
        Other = 3
    }

//*** document class, users can be have many documents.
    public class Document
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string Path { get; set; }
        public DocumentCategory Category { get; set; }
    }


//*** Document Controller - Create Action

// POST: /Document/Create
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create([Bind(Include="Id,Name,Path,Category")] Document document)
        {
            if (ModelState.IsValid)
            {                
                db.Documents.Add(document);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(document);
        }


That's correct? if yes, how i change my DocumentController, Create Action to do correct insert?


someone can help me? Thanks
Posted
Comments
JoCodes 26-Feb-14 23:29pm    
Check this http://stackoverflow.com/questions/19936433/asp-net-mvc-5-identity-application-user-as-foreign-key
EduChapow 27-Feb-14 11:57am    
that's help me with my controller... and my models? what i do? lol
JoCodes 28-Feb-14 7:19am    
In your Model you can always have an UserProfile reference as a foreign key

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900