Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
192 views
in Technique[技术] by (71.8m points)

c# - Entity Splitting with Entity Framework 6 and not identical column names

Hey together I hope one of you can help me. I have a big problem with entity splitting in EF6.

The following scenario. I have to tables like shown in the Screenshot.

Database Tables

The column FUNKTIONSGRUPPEN_ID from table UNTERSTATIONEN references the column ID from table FUNKTIONSGRUPPEN.

Now i want to merge some properties from both tables to one entity in my EF-Model. For this i'm using this code.

public class UNTERSTATIONEN
{
    public long? ID { get; set; }
    public string ZUSATZ_WERT1 { get; set; }
    public string FUNKTIONGSGRUPPEN_ZUSATZWERT { get; set; }
    public long? FUNKTIONGSGRUPPEN_ID { get; set; }
} 

and in the OnModelCreating Function i have this code.

modelBuilder.Entity<UNTERSTATIONEN>()
            .Map(m =>
                      {
                          m.Properties(p =>
                          new
                          {
                              p.FUNKTIONSGRUPPEN_ID,                                 
                              p.ZUSATZ_WERT1,
                              p.ID
                          });
                          m.ToTable(UNTERSTATIONEN);
                      })
            .Map(m =>
                    {
                        m.Property(t => t.FUNKTIONSGRUPPEN_ID).HasColumnName("ID");
                        m.Property(t => t.FUNKTIONGSGRUPPEN_ZUSATZWERT1).HasColumnName("ZUSATZ_WERT1");
                        m.Properties(e =>
                        new
                        {
                            e.FUNKTIONSGRUPPEN_ID,
                            e.FUNKTIONGSGRUPPEN_ZUSATZWERT1
                        });
                        m.ToTable("FUNKTIONSGRUPPEN");
                    });

Loading the entitis like this

using (var dbCtx = new DBContext())
{
   var completeList = dbCtx.UNTERSTATIONEN.ToList();
}

Results in the following error message:

System.InvalidOperationException: 'The entity types 'UNTERSTATIONEN' and 'FUNKTIONSGRUPPEN' cannot share table 'FUNKTIONSGRUPPEN' because they are not in the same type hierarchy or do not have a valid one to one foreign key relationship with matching primary keys between them.'

I looked into alot of blogs and questions on stackoverflow but all not facing my problem that the columns used for mapping are not having the same name and one column is not a primary key of the table.

So what is to do to get this working?

question from:https://stackoverflow.com/questions/65940561/entity-splitting-with-entity-framework-6-and-not-identical-column-names

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...