EF Code First First-to-One

时间:2013-01-30 16:02:34

标签: asp.net-mvc entity-framework-4 ef-code-first entity-relationship

我找到了成千上万的codefirst关系的例子,但我不能做我的样本!

许多错误都是这样的:

类型为“BL.Objects.User”的属性“LanguageID”上的ForeignKeyAttribute无效。在依赖类型“BL.Objects.User”上找不到导航属性“Language”。 Name值应该是有效的导航属性名称。

同样相同......

我真的想加载与用户的语言关联。 (en,ru,es)

public abstract class BaseUser : FinanceBase<int>, IUser
{      
    [ForeignKey("Language")]
    public int LanguageID { get; set; }

    [ForeignKey("LanguageID")]
    public virtual Language Language { get; private set; }        

}

public class User : BaseUser
{
    public override void GenerateID()
    {
        ...
    }
}

public abstract class BaseLanguage : FinanceBase<int>, ILanguage
{
    #region Implementation of ILanguage

    public string Code { get; set; }
    public string Fullname { get; set; }
    public string ImagePath { get; set; }

    #endregion
}

public class Language : BaseLanguage
{
    public override void GenerateID()
    {

    }
}

public class FinanceDatabaseContext : DbContext
{
    public FinanceDatabaseContext()
    {
        Database.SetInitializer(new FinanceContextInitializer());    
    }

    public DbSet<User> Users { get; set; }
    public DbSet<Language> Languages { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<User>().Map(m =>
            {
                m.MapInheritedProperties();
                m.ToTable("Users");
            }).HasKey(x => x.ID).HasRequired(x => x.Language).WithMany().HasForeignKey(x => x.LanguageID);

        modelBuilder.Entity<Language>().Map(m =>
        {
            m.MapInheritedProperties();
            m.ToTable("Languages");
        }).HasKey(x => x.ID);

        base.OnModelCreating(modelBuilder);
    } 
}

public class FinanceContextInitializer : DropCreateDatabaseIfModelChanges<FinanceDatabaseContext>
{
    protected override void Seed(FinanceDatabaseContext context)
    {
        context.Database.ExecuteSqlCommand("ALTER TABLE Users ADD CONSTRAINT uc_Language UNIQUE(LanguageID)");
    }
}

谢谢!

3 个答案:

答案 0 :(得分:1)

您不需要为LanguageID

使用外键
    public int LanguageID { get; set; }
    [ForeignKey("LanguageID ")]
    public virtual Language Language { get; private set; }  

答案 1 :(得分:0)

将虚拟导航字段添加到User表中以获取语言(如果尚未存在)。 即你在POCO类用户中有LanguageId和Language。 另一种方法是将语言类中的导航字段返回给用户, 即公共虚拟列表用户

但是请坚持使用具有虚拟导航属性和外键ID字段的用户。

 ... the rest of User.....
    public int LanguageId
// nav relationship
    public virtual Language Language { set; get; }

// Now the FK declaration as you described should work in fluent API...
// Has required NAVIGATION property, its 1 to many and the I have a field for this FK value called X

modelBuilder.Entity<User>().Map(m =>
        {
            m.MapInheritedProperties();
            m.ToTable("Users");
        }).HasKey(x => x.ID).HasRequired(x => x.Language).WithMany().HasForeignKey(x => x.LanguageID);

答案 2 :(得分:0)

解决。我的导航属性语言有私有 setter

[ForeignKey("LanguageID")]
public virtual Language Language { get; private set; }