简体   繁体   中英

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.

Database erd: 这是ERD

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

Okay, the problem is; how to map DailyLog database entity to the DailyLog C# domain class. Because of performance optimalization we choose to store HeartRate in the database as a 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";

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. And I think there is a much cleaner way to do this. But I don't know how.

You could use 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");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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