简体   繁体   English

外键列名称不同时的实体框架映射

[英]Entity framework mapping when foreign key column name is different

I have a legacy table like this: 我有一个像这样的遗留表:

Country
- countryCode (PK, varchar(10), not null)

Now I have a new table: 现在我有了一张新桌子:

Store
- store_id
- country_code

My models: 我的模特:

public class Country
{
   [Key]
   [Column("countryCode")
   public int CountryCode {get;set;}
}


public class Store
{
   [Key]
   [Column("store_id")
   public int Id {get;set;}

   [Column("country_code")]
   public int CountryCode {get;set;}
}

Now I want to be able to do this: 现在我希望能够做到这一点:

var store = // get store

store.Country.CountryCode

How can I create this mapping? 我该如何创建这个映射? Notice that the column names are not the same (I can't change this). 请注意,列名称不相同(我无法更改)。

I believe I have to add this to my Store model, but how do I specificy the foreign key's seeing as they have different names? 我相信我必须将它添加到我的商店模型中,但是如何确定外键的具体名称呢?

public virtual CountryCode {get;set;}

If your database column has a type of varchar(10) you cannot use an int property in your model but you must use a string , no matter if the property name matches the column name or not. 如果数据库列的类型为varchar(10) ,则无法在模型中使用int属性,但无论属性名称是否与列名匹配,都必须使用string Also in order to be able to access the Country from the Store you need a navigation property Store.Country : 另外,为了能够从Store访问Country ,您需要导航属性Store.Country

public class Country
{
    [Key]
    [Column("countryCode", TypeName = "varchar")]
    [MaxLength(10)]
    public string CountryCode { get; set; }
}

public class Store
{
    [Key]
    [Column("store_id")
    public int Id { get; set; }

    [Column("country_code", TypeName = "varchar")]
    [MaxLength(10)]
    [Required]
    [ForeignKey("Country")]
    public string CountryCode { get; set; }

    public virtual Country Country { get; set; }
}

(It's possible that the ForeignKey attribute is not necessary. You can try it without it. Also remove the [Required] attribute if the country_code column in table Store allows NULL values.) (可能没有必要使用ForeignKey属性。没有它可以尝试。如果表Storecountry_code列允许NULL值,也可以删除[Required]属性。)

You should be able now to access the CountryCode with for example: 您现在应该能够访问CountryCode ,例如:

var store = context.Stores.Find(1);
string countryCode = store.Country.CountryCode;

The store.Country navigation property will be loaded automatically via lazy loading (hence the virtual modifier) when you access the property. 当您访问该属性时, store.Country导航属性将通过延迟加载(因此为virtual修饰符)自动加载。

The name has nothing to do with association (although the same name makes the db more intuitive). 该名称与关联无关(尽管同名使db更直观)。 Through the emdx model view thing there is a form with which you can specify associations. 通过emdx模型视图,可以使用一个表单来指定关联。

Here's a handy tutorial on msdn; 这是一个关于msdn的方便教程; http://blogs.msdn.com/b/efdesign/archive/2009/03/16/foreign-keys-in-the-entity-framework.aspx http://blogs.msdn.com/b/efdesign/archive/2009/03/16/foreign-keys-in-the-entity-framework.aspx

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

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