简体   繁体   English

EF代码第一列名称

[英]EF Code First Column Names

I have the following class: 我有以下课程:

public class CompanyInfo
    {
        [Key]
        public string CompanyId { get; set; }
        public string CompanyName { get; set; }
        public Address CompanyAddress  { get; set; }
    }

With the corresponding Address class: 使用相应的Address类:

public class Address
    {
        public string StreetAddress { get; set; }
        public string StreetAddress2 { get; set; }
        public string City { get; set; }
        public string State { get; set; }
        public string Zip { get; set; }
        public string Plus4 { get; set; }
    } 

Here lies my problem ... the address now generates column names of type: 这是我的问题所在...地址现在生成类型的列名称:

CompanyAddress_StreetAddress

where I would prefer something along the line of: 我更喜欢以下方面的东西:

PrimaryStreetAddress

Given that the members of the Address class are not readily accessible for use with Data Annotation (using Domain Model/ViewModel structure) and are mapped in the controller as such: 假定Address类的成员不易访问以与数据注释一起使用(使用Domain Model / ViewModel结构),并按如下方式映射在控制器中:

var company = new CompanyInfo
            {
                CompanyId = GenerateAccountNumber(),
                CompanyName = addCompany.CompanyName,
                CompanyAddress = new Address
                {
                    StreetAddress = addCompany.StreetAddress,
                    StreetAddress2 = addCompany.StreetAddress2,
                    City = addCompany.City,
                    State = addCompany.State,
                    Zip = addCompany.Zip,
                    Plus4 = addCompany.Plus4
                },

                  -- some other fields here --

            };

I know that the ViewModel is not the proper venue as it is just being mapped in the controller back to the model. 我知道ViewModel是不合适的场所,因为它只是在控制器中映射回模型。

What would be the proper way to set the column names for the reusable class 'Address'? 设置可重用类“地址”的列名的正确方法是什么? I'm assuming that this could be done through FluentAPI if Entity Framework but I am uncertain in how to proceed. 我假设可以通过FluentAPI(如果使用Entity Framework)完成此操作,但是我不确定该如何进行。

Any help would be greatly appreciated. 任何帮助将不胜感激。

Use ForeignKey data annotation so that ef does not create its own foreign key with its own convention. 使用ForeignKey数据注释,以便ef不会使用自己的约定创建自己的外键。

public class CompanyInfo
{
    [Key]
    public string CompanyId { get; set; }
    public string CompanyName { get; set; }
    public Address CompanyAddress  { get; set; }
    [ForeignKey("CompanyAddress")]
    public string PrimaryStreetAddress  { get; set; }
}

You can override in your context method OnModelCreating to specify the relation between CompanyInfo and Address , and you can specify the foreign key name that will identify the relation. 您可以在context方法OnModelCreating以指定CompanyInfoAddress之间的关系,并且可以指定将标识该关系的外键名称

public class DataContext : DbContext {

    //other properties

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
         modelBuilder.Entity<CompanyInfo>()
            .HasRequired(m => m.CompanyAddress)
            .WithOptional()
            .Map(m => { m.MapKey("CompanyAddressId"); });
    }
}

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

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