Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/maven/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# EntityFramework:是否可以将两个不同的列映射到遵循相同实践的相同实体/模型?_C#_Entity Framework_Entity Framework 6 - Fatal编程技术网

C# EntityFramework:是否可以将两个不同的列映射到遵循相同实践的相同实体/模型?

C# EntityFramework:是否可以将两个不同的列映射到遵循相同实践的相同实体/模型?,c#,entity-framework,entity-framework-6,C#,Entity Framework,Entity Framework 6,我正在构建一个应用程序,它从现有表中获取数据,然后将其拆分为多个实体,并遵循特定的趋势。它适用于管理多个不同机构的出租代理。我已确定的一个共享实体是由业主、出租代理和实际财产共享的地址,所有这些实体共享以下财产: public class Address { public string HouseNumber public string FlatPosition public string AddressLine publ

我正在构建一个应用程序,它从现有表中获取数据,然后将其拆分为多个实体,并遵循特定的趋势。它适用于管理多个不同机构的出租代理。我已确定的一个共享实体是由业主、出租代理和实际财产共享的地址,所有这些实体共享以下财产:

public class Address 
    {
        public string HouseNumber
        public string FlatPosition
        public string AddressLine
        public string Town
        public string County
        public string Postcode
    }
在巨表中,它们遵循上述模式,但在前面指定了相关名词,例如。 “房东户号等、房产房号等、出租人房号等”

是否可以从表中获取数据,并使用address类实例化,同时能够区分它们所表示的实体?或者我需要为每个地址设置一个地址类

编辑:


据我所知,不可能根据您的情况使用一个模型类。但是,您可以为所有
地址
型号创建一个通用接口:

公共接口
{
字符串HouseNumber{get;set;}
字符串位置{get;set;}
字符串地址行{get;set;}
字符串{get;set;}
字符串country{get;set;}
字符串邮政编码{get;set;}
}
然后添加模型类,如下所示:

公共类房东地址:IAddress
{
[栏(“房东号”)]
公共字符串HouseNumber{get;set;}
...
}
这样,您至少可以编写更多的通用代码来操作这些类的实例。

1。在数据库中折叠 您可以创建一个视图或创建一个查询,将这3个列折叠成一个合理的列

2.收回时折叠 2a Linq到sql 您可以做一个简单的concat:

await db.Addresses
  .Select( a=> new { Address = AHouseNumber + BHouseNumber + CHouseNumber } )
  .ToListAsync(); 

2b。根据需要运行自定义查询,您需要为地址定义一个
复杂类型
,然后将属性映射到相应的列

public class Address 
{
    public string HouseNumber { get; set; }
    public string FlatPosition { get; set; }
    public string AddressLine { get; set; }
    public string Town { get; set; }
    public string County { get; set; }
    public string Postcode { get; set; }
}

public YourEntity
{
    public int Id { get; set; } // or whatever you use
    // other properties

    // complex type properites
    public Address LandlordAddress { get; set; }
    public Address LandlordAgentAddress { get; set; }
}
然后在
DbContext
中配置:

protected void OnModelCreating( DbModelBuilder modelBuilder )
{
    // configure like an entity but use .ComplexType<>()
    modelBuilder.ComplexType<Address>()
        // e.g. set max size of one of the properties
        .Property( a => a.Postcode )
        .HasMaxLength( 10 );
    // etc.

    // map properties to columns if you don't want to use default naming convention
    modelBuilder.Entity<YourEntityType>()
        .Property( yet => yet.LandlordAddress.Postcode )
        .HasColumnName( "LandlordPostcode" );

    modelBuilder.Entity<YourEntityType>()
        .Property( yet => yet.LandordAgentAddress.Postcode )
        .HasColumnName( "LandlordAgentPostcode" );
}
模型创建时受保护的void(DbModelBuilder modelBuilder)
{
//像实体一样配置,但使用.ComplexType()
modelBuilder.ComplexType()
//例如,设置其中一个属性的最大大小
.Property(a=>a.邮政编码)
.HasMaxLength(10);
//等等。
//如果不想使用默认命名约定,请将属性映射到列
modelBuilder.Entity()
.Property(yet=>yet.landordaddress.Postcode)
.HasColumnName(“landordPostcode”);
modelBuilder.Entity()
.Property(yet=>yet.LandordAgentAddress.Postcode)
.HasColumnName(“LandorAgentPostCode”);
}

包括一个示例表定义和您希望转换为的实体定义。我认为你想做的事情可以通过复杂的类型来完成,但我们需要更多details@Moho我添加了表格标题,由于GDPR,我不得不排除数据。这是用于导出到CSV的吗?不是,它用于在前端显示。基本上是一个小应用程序,允许用户看到谁住在哪里,谁是房东等。感谢您的回复!通过在顶部标记列名,然后连接到一个已经存在的表,这还会带来相关数据吗?我一直很困惑EF实际上是如何将现有数据迁移到应用程序中的。是的,模型属性将映射到不同名称的数据库表列。如果您使用的是数据库优先的方法,并且您的C#项目和数据库之间的命名约定不同,那么它通常会派上用场。
protected void OnModelCreating( DbModelBuilder modelBuilder )
{
    // configure like an entity but use .ComplexType<>()
    modelBuilder.ComplexType<Address>()
        // e.g. set max size of one of the properties
        .Property( a => a.Postcode )
        .HasMaxLength( 10 );
    // etc.

    // map properties to columns if you don't want to use default naming convention
    modelBuilder.Entity<YourEntityType>()
        .Property( yet => yet.LandlordAddress.Postcode )
        .HasColumnName( "LandlordPostcode" );

    modelBuilder.Entity<YourEntityType>()
        .Property( yet => yet.LandordAgentAddress.Postcode )
        .HasColumnName( "LandlordAgentPostcode" );
}