简体   繁体   English

使用实体框架一对一的外键

[英]Foreign key one to one with Entity Framework

The Customer can have only one Language . Customer只能使用一种Language I don't find the right way to create the key. 我找不到创建密钥的正确方法。 When I get an object Customer the property LanguageId has a content but not the property Language . 当我得到Customer对象时,属性LanguageId具有内容,但没有属性Language I use EF 6.1 我使用EF 6.1

This Language object will be use in other object. Language对象将在其他对象中使用。

I did this : 我是这样做的:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
    modelBuilder.Configurations.Add(new CustomerMap());
    modelBuilder.Configurations.Add(new LanguageMap());   
}

public class Customer
{
    public int CustomerID { get; set; }
    public string Code { get; set; }

    public int LanguageId { get; set; }
    [ForeignKey("LanguageId")]
    public Language Language { get; set; }
}

public class CustomerMap : EntityTypeConfiguration<Customer>
{
    public CustomerMap()
    {
        this.HasKey(t => t.CustomerID);

        // Properties
        this.Property(t => t.CustomerID).IsRequired();
        this.Property(t => t.Code).IsRequired();

        // Table & Column Mappings
        this.ToTable("Customer");
        this.Property(t => t.CustomerID).HasColumnName("CustomerID");
        this.Property(t => t.Code).HasColumnName("Code");
    }
}

public class Language
{
    public int LanguageID { get; set; }
    public string Code { get; set; }
}

public class LanguageMap : EntityTypeConfiguration<Language>
{
    public LanguageMap()
    {
        this.HasKey(t => t.LanguageID);
        this.Property(t => t.Code).IsRequired();
    }
}

Update (Language will be used in other object) 更新(将在其他对象中使用语言)

在此处输入图片说明

You can achieve one to one with two options, but first you have to remove the foreign key value in the principal. 您可以使用两个选项一对一地实现,但首先必须删除主体中的外键值。 Principal means that the record must exist first, that's why this entity doesn't need to have foreign key value, just foreign key reference. Principal意味着必须首先存在记录,这就是为什么此实体不需要具有外键值,而只需具有外键引用的原因。

Remove this code. 删除此代码。

 public int LanguageId { get; set; } [ForeignKey("LanguageId")] 

First . 第一 After removing above code add this configuration. 删除上面的代码后,添加此配置。

 modelBuilder.Entity<Customer>() .HasRequired(a => a.Language) .WithRequiredPrincipal(); 

Second , also add the foreign key reference on dependent ( Language ). 其次 ,还要在依赖项( Language )上添加外键引用。

 public Customer Customer { get; set; } 

Then mention the principal reference in WithRequiredPrincipal . 然后在WithRequiredPrincipal提到主体引用。

 modelBuilder.Entity<Customer>() .HasRequired(a => a.Language) .WithRequiredPrincipal(x => x.Customer); 

update 更新

To load language you can do it with lazy loading by adding virtual keyword (the context configuration must also enable lazy loading and proxy). 要加载语言,您可以通过添加virtual关键字来进行延迟加载(上下文配置还必须启用延迟加载和代理)。

 public virtual Language Language { get; set; } 

Or do with eager loading. 或与渴望加载。

 var customerID = 5; var customer = db.Set<Customer>().Include(c => c.Language) .FirstOrDefault(c => c.CustomerID == customerID); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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