简体   繁体   English

实体框架Fluent API将DbSet <>值映射到表中的自定义对象

[英]Entity Framework Fluent API map DbSet<> value to custom object in table

The title may be a bit confusing. 标题可能有点混乱。 But the problem is: 但是问题是:

Due database optimalization we have choose to design an ERD for the database which is in some aspects different from the class diagram. 由于数据库的优化,我们选择为数据库设计ERD,这在某些方面与类图有所不同。

Database erd: 数据库erd: 这是ERD

Class diagram: 类图: 在此处输入图片说明

Okay, the problem is; 好的,问题是; how to map DailyLog database entity to the DailyLog C# domain class. 如何将DailyLog数据库实体映射到DailyLog C#域类。 Because of performance optimalization we choose to store HeartRate in the database as a CLOB. 由于性能的优化,我们选择将HeartRate作为CLOB存储在数据库中。 And in the domain classes, it is easier to have the object in a list of a specific class type. 在域类中,将对象包含在特定类类型的列表中会更容易。

How to get this done in a nice and clean way? 如何以一种干净利落的方式完成这项工作? I really don't know how to map this kind of database entity(s) to the domain classes. 我真的不知道如何将这种数据库实体映射到域类。

The plan is to store heartrates this way: String: "07:00:01,50;07:00:02,52"; 计划是这样存储心率的:字符串:“ 07:00:01,50; 07:00:02,52”;

I have experimentend with DTO's but then I have to create a whole new layer of DTO's between Domain classes and Entity Framework. 我已经完成了DTO的实验,但是随后我必须在Domain类和Entity Framework之间创建一个全新的DTO层。 And I think there is a much cleaner way to do this. 而且我认为有一种更清洁的方法可以做到这一点。 But I don't know how. 但是我不知道如何。

You could use EF Fluent API. 您可以使用EF Fluent API。 Check this link for details. 检查链接以获取详细信息。 In your case the mapping should be something like this: 在您的情况下,映射应如下所示:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity<DailyLog>().Map(m =>
    {
        m.Properties(p => new { p.Id, p.Date});
        m.ToTable("DAILYLOG");
    });

    modelBuilder.Entity<HeartRatePattern>().ToTable("DAILYLOG").Property(x => string.Join(";",x.HeartRate)).HasColumnType("varchar");
}

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

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