[英]Using Updatable Views with Entity Framework Code First
I am learning to use Entity Framework (code first). 我正在学习使用实体框架(代码优先)。 Everything was going well until I realized I need to interact with an existing updatable view. 一切进展顺利,直到我意识到我需要与现有的可更新视图进行交互。 I have confirmed the view can be updated in SSMS. 我已经确认可以在SSMS中更新视图。
There are two entities involved: AppUser (Identity User) and FacilityView. 涉及两个实体:AppUser(身份用户)和FacilityView。
public class AppUser : IdentityUser
{
// additional props
public virtual ICollection<FacilityView> Facilities { get; set; }
}
public class FacilityView
{
[HiddenInput(DisplayValue=false)]
public int ID { get; set; }
public string Name { get; set; }
public string County { get; set; }
public string WebSiteURL { get; set; }
public bool IsActive { get; set; }
public virtual ICollection<AppUser> Users { get; set; }
}
I've used navigation properties to relate the two, thinking EF will build the join table when I run the next migration. 我已经使用导航属性将两者关联起来,以为当我运行下一个迁移时EF将建立联接表。 I've read that EF treats views as read-only, so I tried to override this by creating my own EntityTypeConfiguration class: 我已经读过EF将视图视为只读,因此我尝试通过创建自己的EntityTypeConfiguration类来覆盖它:
public class FacilityViewConfiguration : EntityTypeConfiguration<FacilityView>
{
public FacilityViewConfiguration()
{
this.HasKey(t => t.ID);
this.ToTable("FacilityViews");
}
}
The configuration is referenced in my context class inside the OnModelCreating method. 该配置在OnModelCreating方法的上下文类中引用。
public class AppIdentityDbContext : IdentityDbContext<AppUser>
{
public AppIdentityDbContext() : base("DbConn") { }
public DbSet<Facility> Facilities { get; set; }
public DbSet<FacilityView> FacilityDetails { get; set; }
static AppIdentityDbContext()
{
Database.SetInitializer<AppIdentityDbContext>(new DbConnInit());
}
public static AppIdentityDbContext Create()
{
return new AppIdentityDbContext();
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
// TRYING TO TRICK EF HERE
modelBuilder.Configurations.Add(new FacilityViewConfiguration());
}
}
When I add a migration, this is the resulting Up and Down: 当我添加迁移时,这就是最终的结果:
public partial class UserFacilityJoinTable : DbMigration
{
public override void Up()
{
CreateTable(
"dbo.AppUserFacilityViews",
c => new
{
AppUser_ID = c.String(nullable: false, maxLength: 128),
FacilityViewsID = c.Int(nullable: false),
})
.PrimaryKey(t => new { t.AppUser_ID, t.FacilityViewsID })
.ForeignKey("dbo.AspNetUsers", t => t.AppUser_ID, cascadeDelete: true)
.ForeignKey("dbo.FacilityViews", t => t.FacilityViewsID, cascadeDelete: true)
.Index(t => t.AppUser_ID)
.Index(t => t.FacilityViewsID);
}
public override void Down()
{
DropForeignKey("dbo.AppUserFacilityViews", "FacilityViewsID", "dbo.FacilityViews");
DropForeignKey("dbo.AppUserFacilityViews", "AppUser_ID", "dbo.AspNetUsers");
DropIndex("dbo.AppUserFacilityViews", new[] { "FacilityViewsID" });
DropIndex("dbo.AppUserFacilityViews", new[] { "AppUser_ID" });
DropTable("dbo.AppUserFacilityViews");
}
}
It is attempting to create a new join table like I wanted, but when I run the Update-Database command, I get the following error: 它试图像我想要的那样创建一个新的联接表,但是当我运行Update-Database命令时,出现以下错误:
Foreign key 'FK_dbo.AppUserFacilityViews_dbo.FacilityViews_FacilityViewsID' references object 'dbo.FacilityViews' which is not a user table. 外键“ FK_dbo.AppUserFacilityViews_dbo.FacilityViews_FacilityViewsID”引用的对象“ dbo.FacilityViews”不是用户表。 Could not create constraint. 无法创建约束。 See previous errors. 请参阅先前的错误。
I thought I had tricked EF into treating the view like a table, but it's obviously not buying it. 我以为我欺骗了EF将视图当作桌子来对待,但是显然不买它。 I'm wondering if I missed something or maybe taking the wrong approach. 我想知道我是否错过了什么,或者采取了错误的方法。 Any help will be greatly appreciated. 任何帮助将不胜感激。 I've been stuck here for a few days. 我被困在这里几天了。
AFAIK you can't create a constraint to/from a view (even a materialized one, exception of index). AFAIK,您不能为视图创建约束或从视图创建约束(即使是实例化的,索引除外)。
But you can model your context without database FK. 但是您可以在没有数据库FK的情况下为上下文建模。 You then have to handle the integrity at context or application level 然后,您必须在上下文或应用程序级别处理完整性
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.