【发布时间】:2011-05-05 23:26:57
【问题描述】:
所以首先我不知道我使用的是什么实体框架版本。我假设它是 4 但我该如何检查?
其次,我在 web.config 中有以下连接字符串:
<connectionStrings>
<add name="DBEntites"
connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true"
providerName="System.Data.SqlClient" />
<add name="ApplicationServices"
connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true"
providerName="System.Data.SqlClient" />
如您所见,它们是同一个数据库。实际上它们是由 asp.net Membership Provider 创建的数据库。
现在我有以下模型:
public class Profile {
[Key]
public string username { get; set; }
[Display(Name = "Full Name")]
public string FullName { get; set; }
}
够简单吗?
然后我有以下连接到数据库的类:
public class DBEntities : DbContext {
public DbSet<Profile> Profiles { get; set; }
}
然后在我的 Account Controller 中,实际上这与您选择新的 MVC3 项目时 VS2010 创建的控制器相同。
[HttpPost]
public ActionResult Register(RegisterModel model) {
if (ModelState.IsValid) {
// Attempt to register the user
MembershipCreateStatus createStatus;
Membership.CreateUser(model.UserName, model.Password, model.Email, null, null, true, null, out createStatus);
Profile pm = new Profile();
pm.username=model.UserName;
pm.FullName="unknown";
db.Profiles.Add(pm);
db.SaveChanges();
if (createStatus == MembershipCreateStatus.Success) {
FormsAuthentication.SetAuthCookie(model.UserName, false /* createPersistentCookie */);
return RedirectToAction("Index", "Home");
} else {
ModelState.AddModelError("", ErrorCodeToString(createStatus));
}
}
// If we got this far, something failed, redisplay form
return View(model);
}
所以当我运行这个应用程序时,它会毫无例外地运行。如果我添加一个新用户,新用户将被添加到 asp.net 成员表中。因此,据我了解,Entity 框架应该创建一个名为“Profile”的表,其中包含“username”和“fullname”两列。
但是,事实并非如此。为什么代码可以无异常单步通过,db.SaveChanges()可以通过,却没有创建表?
我做错了什么?
编辑:我认为该表存在是因为如果我尝试使用相同的用户名创建一个条目(显然不能,因为它是主键),它会抛出一个异常,说明该条目已经存在。
但我似乎找不到桌子!
【问题讨论】:
标签: entity-framework asp.net-mvc-3 entity-framework-4.1