[英]Error with Entity Framework 4 and MVC 3
I have a database with 3 tables: 我有一个包含3个表的数据库:
Then I added the connection string to web.config
and created an EF with the following classes: 然后,我将连接字符串添加到
web.config
并使用以下类创建了EF:
namespace MySite.Models
{
public class MySiteDBModel : DbContext
{
public DbSet<Topic> Topics { get; set; }
public DbSet<Subject> Subjects { get; set; }
public DbSet<Member> Members { get; set; }
public DbSet<TopicDataModel> TopicDataModel { get; set; }
protected override void OnModelCreating(DbModelBuilder mb)
{
mb.Conventions.Remove<PluralizingTableNameConvention>();
}
}
public class Topic
{
[Key]
public int TopicID { get; set; }
public int SubID { get; set; }
public int MemberID { get; set; }
public string TDate { get; set; }
public string Title { get; set; }
public string FileName { get; set; }
public int Displays { get; set; }
public string Description { get; set; }
public virtual Subject Subject { get; set; }
public virtual Member Member { get; set; }
public virtual ICollection<TopicView> TopicView { get; set; }
}
public class Subject
{
[Key]
public int SubID { get; set; }
public string SubName { get; set; }
public virtual ICollection<Topic> Topic { get; set; }
}
public class Member
{
[Key]
public int MemberID { get; set; }
public string FLName { get; set; }
public string Email { get; set; }
public string Pwd { get; set; }
public string About { get; set; }
public string Photo { get; set; }
public virtual ICollection<Topic> Topic { get; set; }
}
public class TopicDataModel
{
[Key]
public int TopicID { get; set; }
public string SubName { get; set; }
public string FLName { get; set; }
public string TDate { get; set; }
public string Title { get; set; }
public int Displays { get; set; }
public string Description { get; set; }
}
}
Now when I am trying to query the database with the this code: 现在,当我尝试使用以下代码查询数据库时:
public ActionResult Index()
{
var topics = from t in db.Topics
join s in db.Subjects on t.SubID equals s.SubID
join m in db.Members on t.MemberID equals m.MemberID
select new TopicDataModel()
{
TopicID = t.TopicID,
SubName = s.SubName,
FLName = m.FLName,
TDate = t.TDate,
Title = t.Title,
Displays = t.Displays,
Description = t.Description
};
return View(topics.ToList());
}
I got this Error: 我收到此错误:
The model backing the 'MySiteDBModel' context has changed since the database was created.
自创建数据库以来,支持“ MySiteDBModel”上下文的模型已更改。 Either manually delete/update the database, or call Database.SetInitializer with an IDatabaseInitializer instance.
手动删除/更新数据库,或使用IDatabaseInitializer实例调用Database.SetInitializer。 For example, the DropCreateDatabaseIfModelChanges strategy will automatically delete and recreate the database, and optionally seed it with new data.
例如,DropCreateDatabaseIfModelChanges策略将自动删除并重新创建数据库,并可以选择用新数据作为种子。
Please help me!!!!!! 请帮我!!!!!!
You need to set some controls on how EF is handling changes to your data model. 您需要对EF如何处理对数据模型的更改进行一些控制。 Julie Lerman has a good blog post on Turning Off Code First Database Initialization Completely .
朱莉勒曼(Julie Lerman)在“完全关闭代码优先数据库初始化”上有一篇不错的博客文章。
Also, here is a good overview - Inside Code First Database Initialization 另外,这是一个很好的概述- 内部代码优先数据库初始化
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.