New Posts New Posts RSS Feed: 1 to 0,1 relation with same key
  FAQ FAQ  Forum Search   Calendar   Register Register  Login Login

1 to 0,1 relation with same key

 Post Reply Post Reply
Author
Walid View Drop Down
Senior Member
Senior Member
Avatar

Joined: 14-Nov-2010
Posts: 161
Post Options Post Options   Quote Walid Quote  Post ReplyReply Direct Link To This Post Topic: 1 to 0,1 relation with same key
    Posted: 14-Dec-2011 at 1:11pm
Having these two classes
    public class User
    {
        [Key]
        public int UserId { get; set; }
        public string UserName { get; set; }
     
        //[InverseProperty("User")]
        public Membership Membership { get; set; }
    }

    public class Membership

    {
        [Key]
        public int UserId { get; set; }
        public string Password { get; set; }
        public string Email { get; set; }
 
        //[InverseProperty("Membership")]
        public User User { get; set; }
    }
InverseProperty attribute seems to not work, so I defined the relation in FluentAPI :
 

modelBuilder.Entity<Membership>()

                .HasRequired(p => p.User)

                .WithOptional(u => u.Membership);

Is that the only way to define such relation ? is there any syntax possible with AOP ?

Regards,
 


Edited by Walid - 14-Dec-2011 at 1:11pm
Back to Top
DenisK View Drop Down
IdeaBlade
IdeaBlade


Joined: 25-Aug-2010
Posts: 715
Post Options Post Options   Quote DenisK Quote  Post ReplyReply Direct Link To This Post Posted: 14-Dec-2011 at 2:15pm
Hi Walid;

Our AOP Code First injection is built on top of Entity Framework Code First engine. So, to answer your question, no, we don't provide our own Code First syntax.

Just recently, we've discovered some weird behaviors with 1 to 1 relationships. Here's a post on Microsoft forum describing this. It should also have answers to your question on how to define 1 to 1 with various Code First attributes.


We also recommend reading Julia Lerman's Code First book. http://juliel.me/ufZtlb

Hope this helps.
Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down