具有多个模型的实体框架引发错误说明“支持< modelDbContext>上下文已改变...“ [英] Entity Framework with multiple models throws error stating "The model backing the <modelDbContext> context has changed..."

查看:103
本文介绍了具有多个模型的实体框架引发错误说明“支持< modelDbContext>上下文已改变...“的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个单独的不相关的模型,我想在同一个数据库中使用。这里是每个的代码:

  public class ImageDBContext:DbContext 
{
public ImageDBContext b $ b:base(DefaultConnection)
{
}

public DbSet< ImageModel>图片{get;组; }

}

[Table(Images)]
public class ImageModel
{
[Key]
public int ID {get;组; }
public string Type {get;组; }
public string标题{get;组; }
public string描述{get;组; }
public string FileName {get;组; }

[NotMapped]
public HttpPostedFileBase Attachment {get;组; }
}

以下是其他型号

  public class TestimonialDBContext:DbContext 
{
public TestimonialDBContext()
:base(DefaultConnection)
{
}

public DbSet< Testimonial>见证{get;组; }
}

[表(Testimonials)]
public class Testimonial
{
[Key]
public int Id {get ;组; }
public string Name {get;组; }
public string描述{get;组; }
}



当我访问一个模型的视图时,它工作得很好。不管哪个模型,但是当我去其他模型的视图,我得到这个错误:



支持'ImageDBContext'上下文的模型已经改变,因为数据库已创建。考虑使用Code First迁移更新数据库( http://go.microsoft.com/fwlink/?hl=zh-CN ?LinkId = 238269 )。



在我看来,两个模型都在尝试使用同一个表格?表格是由初始化的第一个模型创建的,但第二个模型尝试使用现有表格而不是创建自己的表格。



如何获得两个模型在数据库中使用自己的表?



这里是连接字符串:

 < add name =DefaultConnectionconnectionString =Data Source = localhost\SQLEXPRESS;初始目录= --------;用户ID = -------;密码= -------providerName =System.Data.SqlClient/> 



更新:我已经添加了以下代码到模型,我仍然得到相同的错误。 p>

  protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity< ImageModel>()ToTable ImagesTable);
}

  protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity< Testimonial>()ToTable(TestimonialsTable);
}


解决方案

错误消息是欺骗 - 这不能/不应该使用代码先迁移解决。你有两个不同的DbContexts。如果它是同一个数据库,那么只使用一个上下文,您添加了图像和推荐。



你这样做的方式,你基本上说有两个不同的数据库,它们使用相同的连接字符串,每个数据库有一个表,图片推荐。因此,当您访问 Images 页面时,将使用一个表 Images 创建数据库。但是当您访问 Testimonials 时,它会识别数据库结构与一个 Testimonials 表的模型不匹配,并警告您。



创建一个数据库上下文,如下所示:

  public class MyDBContext: DbContext 
{
public DbSet< ImageModel>图片{get;组; }
public DbSet< Testimonial>见证{get;组; }
}

在您的控制器中,只需创建一个新的实例访问DB。如果将连接字符串(在web.config中)命名为MyDbContext,您可以跳过需要调用 base(DefaultConnection)的空构造函数。 >

I have two separate unrelated models that I am trying to use in the same database. Here is the code for each:

public class ImageDBContext : DbContext
{
    public ImageDBContext()
        : base("DefaultConnection")
    {
    }

    public DbSet<ImageModel> Images { get; set; }

}

[Table("Images")]
public class ImageModel
{
    [Key]
    public int ID { get; set; }
    public string Type { get; set; }
    public string Title { get; set; }
    public string Description { get; set; }
    public string FileName { get; set; }

    [NotMapped]
    public HttpPostedFileBase Attachment { get; set; }
}

Here is the other model

public class TestimonialDBContext : DbContext
{
    public TestimonialDBContext()
        : base("DefaultConnection")
    {
    }

    public DbSet<Testimonial> Testimonials { get; set; }
}

[Table("Testimonials")]
public class Testimonial
{
    [Key]
    public int Id { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
}

When I access the view for one model it works great. It doesn't matter which model but when I go to the view for the other model I get this error:

The model backing the 'ImageDBContext' context has changed since the database was created. Consider using Code First Migrations to update the database (http://go.microsoft.com/fwlink/?LinkId=238269).

It seems to me like both models are trying to use the same table? The table gets created by the first model that is initialized but then the second model tries to use the the existing table instead of creating its own.

How can I get both models to use their own tables in the database?

Here is the connection string:

 <add name="DefaultConnection" connectionString="Data Source=localhost\SQLEXPRESS;Initial Catalog=--------;User ID=-------;Password=---------" providerName="System.Data.SqlClient" />

Update: I have added the following code to the models and I still get the same error.

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<ImageModel>().ToTable("ImagesTable");
    }

and

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Testimonial>().ToTable("TestimonialsTable");
    }

解决方案

The error message is deceiving - this cannot/should not be resolved with "Code First Migrations". You have two different DbContexts. If it's the same database then just use one context to which you added both Images and Testimonials.

The way you're doing it, you are basically saying there are two different databases, which use same connection string, and each database has one table, either Images, or Testimonials. So, when you access say Images page, the database gets created with one table Images. But when you access Testimonials it recognizes that the DB structure does not match the model of one Testimonials table and warns you.

Create one DB context like so:

public class MyDBContext : DbContext
{
    public DbSet<ImageModel> Images { get; set; }
    public DbSet<Testimonial> Testimonials { get; set; }
}

And in your controllers just create a new instance of this class when you need to access DB. If you name your connection string (in web.config) as "MyDbContext" you can skip the empty constructor where you need to call base("DefaultConnection").

这篇关于具有多个模型的实体框架引发错误说明“支持&lt; modelDbContext&gt;上下文已改变...“的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆