Print Page | Close Window

1 to 0,1 relation with same key

Printed From: IdeaBlade
Category: DevForce
Forum Name: DevForce 2010
Forum Discription: For .NET 4.0
URL: http://www.ideablade.com/forum/forum_posts.asp?TID=3158
Printed Date: 25-Apr-2024 at 1:02am


Topic: 1 to 0,1 relation with same key
Posted By: Walid
Subject: 1 to 0,1 relation with same key
Date 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,
 



Replies:
Posted By: DenisK
Date 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.

http://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/bb619084-5995-4c80-87bf-0ddc77977066 - http://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/bb619084-5995-4c80-87bf-0ddc77977066

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

Hope this helps.



Print Page | Close Window