映射2代表以单一实体在实体框架

问题描述 投票:4回答:3

我希望你能帮助我。

我在DB 2个表:比尔和BillItem。这些表被配置为与一个到在纸币的原理是表中的一个数据库,而关系是BillItem从属一个

表草案的结构:

Int BillId (PK)
Int BillTypeId Not Null
Varchar(5) Usr Not Null
DateTime Tm Not Null

表BillItem的结构:

Int BillItemId (PK)
Int ItemId Not Null 
Varchar(5) Usr Not Null
DateTime Tm Not Null

我想用流利的API和Entity Framework 4.1代码优先的方式来处理2表映射到一个单一的POCO类

我也希望配置表中的列名称使用在POCO类不同性质的名称(即ID而不是BILLID,用户代替USR)

这是一个传统的数据库,我不能修改任何对象。

如何才能做到这一点?

谢谢你们。

生成的类应该是(如果可能):

public int Id {get;set;}
public int BillTypeId {get;set;}
public int ItemId {get;set;}
public string User {get;set;}
public string User1 {get;set;}
public DateTime Tm {get;set;}
public DateTime Tm1 {get;set;}
c# entity-framework ef-code-first code-first fluent-interface
3个回答
0
投票

对于重命名列,这很简单:

[Table("SomeHorribleTableName")]
    public class MyNiceTableName
    {
        [Column("Usr")]
        public string User { get; set; }
    }

在这里,我改名为实体以及..没有必要保持一个可怕的表名。关于映射表2比1名的实体。我不认为这是可能的。在这里看到:Mapping data from 2 tables to 1 entity - Entity Framework 4

流畅的风格:

public class Bill
    {
        public int ID { get; set; }
        public int BillTypeID { get; set; }
        public string UserName { get; set; }
        public DateTime Time { get; set; }
    }

    public class BillItem
    {
        public int ID { get; set; }
        public int ItemID { get; set; }
        public string UserName { get; set; }
        public DateTime Time { get; set; }
    }

    internal class BillMap : EntityTypeConfiguration<Bill>
    {
        public BillMap()
        {
            ToTable("Bills");
            HasKey(x => x.ID);
            Property(x => x.ID).HasColumnName("BillId");
            Property(x => x.BillTypeID).IsRequired().HasColumnName("BillTypeId");
            Property(p => p.UserName).IsRequired().HasColumnName("Usr");
            Property(x => x.Time).IsRequired().HasColumnName("Tm");
        }
    }

    internal class BillItemMap : EntityTypeConfiguration<BillItem>
    {
        public BillItemMap()
        {
            ToTable("BillItems");
            HasKey(x => x.ID);
            Property(x => x.ID).HasColumnName("BillItemId");
            Property(x => x.ItemID).IsRequired().HasColumnName("ItemId");
            Property(p => p.UserName).IsRequired().HasColumnName("Usr");
            Property(x => x.Time).IsRequired().HasColumnName("Tm");
        }
    }

这可能是我能做到的最好。至于处理两个表中的列USR,那将是什么你应该在你自己的控制器类/业务逻辑层处理。

还有一两件事:对于配置类以上..叫他们在你的DbContext像这样:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Configurations.Add(new BillMap());
    modelBuilder.Configurations.Add(new BillItemMap());
}

0
投票

我认为你可以做到这一点与实体分割功能,但你需要使用一些数据注释(无esacpe): 警告:我还没有尝试过这种方法,但它可能工作,检查出来,如果你们愿意先:注释属于BillItem表中你单域模型类与HasColumnName数据注解的属性。 第二:使用此代码示例

public class YourSingleModelClassNameConfiguration : EntityTypeConfiguration<YourSingleModelClassName> {
    public YourSingleModelClassNameConfiguration() {

        ToTable("Bill"); Property(param => param.Id).IsRequired().HasColumnName("BillId");

        // NOW REPEAT THAT FOR ALL BILL PROPERTIES WITH YOUR REQUIRED ATTRIBUTES (ISREQUIRED OR NOT, LENGTH ...)

        // NOW THE PROPERTIES YOU NEED TO MAP TO THE BILL ITEMS TABLE GOES INTO THE MAP FUNCTION
        Map(
        param =>
            { param.Properties(d => new {d.ItemId, d.User1}); m.ToTable("BillItem");
            }
        );

        // DON'T FORGET TO MENTION THE REST OF THE PROPERTIES BELONGING TO THE BillItem TABLE INSIDE THE PROPERTIES METHOD IN THE LAST LINE.

    }
}

0
投票

对于这个问题,你可以很轻松地做到这一点,你有2个选项。第一种是使用实体框架(mapping multiple tables to a single entity class in entity framework)的类映射。

第二个选项是手动做到这一点:2代表必须遵循的限制是1必须是强大的表,第二个需要是弱者(通过这样做,你应该由一个有1只从强表弱),否则,这不会工作的结果将是对象的集合,然后你需要决定的第一个对象时是否是你所需要的人,也没有其他。

如果你想使用一个稍快的方法我建议使用自己的逻辑来映射POCO对象。创建2个clases将与数据库表的映射,并从服务对象建立的对象。

这种做法是很好的,因为所产生的LINQ查询不会使用内部连接,这使得它更快。但加入应该从你的身边保管。

int i=0;
// this 2 refer to the 2 different mapped tables in your context database
var pbdata = _pbdata.GetSingle(p=>p.StrongTableID == StrongTableID);
var pb = _pb.GetSingle(p=>p.WeakTableID == pbdata.WeakTableID);
// you can see that i am looking for the StrongTableID in order to select the entity value

// this is optional but usefull! you can do the
// "copy" inside a function where in future if the 
// object changes, you can update easily
ObjectComposite.Map(body, ref pb, ref pbdata);

// the following proccess needs to be done like this...
// first save the value in the weak object so 
// the WeakTableID is recorded
// UPDATE: maybe these 2 should go into a transact for more security... but... ok...
Save(pb);
i += this.unitOfWork.Save();
pbdata.ProfessionalBodyID = pb.ProfessionalBodyID;

// once the values for the second objects are set, save it all again.
Save(pbdata);
i += this.unitOfWork.Save();
return i > 0 ? true : false;

这种方法是快,但你需要从自己控制一切。好事是,在这里你可以根据需要尽可能多的表映射

希望能帮助到你!

© www.soinside.com 2019 - 2024. All rights reserved.