我有一个SQL视图,我正在使用实体框架。该视图代表两个连接在一起的现有对象。我想最好的办法是让对象继承另一个对象。如何将数据从数据库表中放入具有EF继承类的实体中?
我得到这个错误:
System.Data.Entity.Core.EntityCommandExecutionException: An error occurred while executing the command definition. See the inner exception for details. ---> System.Data.SqlClient.SqlException: Invalid column name 'Discriminator'.
这里是我的设置:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Class1>()
.ToTable("mySQLView", "TEST")
.HasKey(r => new { r.Property1 });
modelBuilder.Entity<Class1>().Property(r => r.Property1).HasColumnName("Key1");
modelBuilder.Entity<Class1>().Property(r => r.Property2).HasColumnName("Column2");
modelBuilder.Entity<Class1>().Property(r => r.Property3).HasColumnName("Column3");
modelBuilder.Entity<Class1>().Property(r => r.Property4).HasColumnName("Column4");
}
这里是我的两个类:
public class Class1 : Class2
{
public string Property1 { get; set; }
public string Property2 { get; set; }
}
public class Class2
{
public string Property3 { get; set; }
public string Property4 { get; set; }
}
Class2中正在被利用分开的DB调用。 Class1仅在其中一个调用中使用。任何关于如何构建我的类或正确设置EF的帮助将不胜感激!
这些方法很有帮助,让我更接近解决方案。虽然在与我的团队交谈之后,我们决定只复制这些属性。尽管它们具有完全相同的结构和命名约定,但这两个视图是独立数据库中的两组独立数据。 –