简体   繁体   中英

Same navigation types one more in EF 4.1

I am a little confused about below entity relations. Because it holds User relation one than more. I am feeling that there is something wrong. Is there anything wrong about it ?

public class Subject: Entity
{
    public Advert()
    {
        CreateDate = DateTime.Now;
    }
    public virtual User Owner{ get; set; }
    public virtual List<User> Voters{ get; set; }
    public virtual List<User> Followers{ get; set; }
}

I'm not sure if this will apply to you Lists, which should be ICollections for EF CodeFirst standards.

you may also want to use inheritance to differentiate different types of users or have different entities for the followers and for the voters.

You need to map this using the WithMany()

this will allow you to specify the foreignKey for the relationship

http://weblogs.asp.net/manavi/archive/2011/01/23/associations-in-ef-code-first-ctp5-part-3-one-to-one-foreign-key-associations.aspx

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.Entity<User>()
                .HasRequired(a => a.BillingAddress)
                .WithMany()
                .HasForeignKey(u => u.BillingAddressId);

    modelBuilder.Entity<User>()
                .HasRequired(a => a.DeliveryAddress)
                .WithMany()
                .HasForeignKey(u => u.DeliveryAddressId);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM