[英]Can't create database with Entity Framework code-first + no database
我正在研究实体框架,我可以使用代码优先+现有数据库。 但是,当我尝试首先使用代码创建一个新数据库时,它不起作用。
你能帮我纠正我的代码吗?
public class QuanLySinhVien : DbContext
{
public virtual DbSet<SinhVien> SinhViens { get; set; }
public virtual DbSet<Group> Groups { get; set; }
}
public class SinhVien
{
public int SinhVienId { get; set; }
public string Name { get; set; }
public string Address { get; set; }
public DateTime BirthDay { get; set; }
public int GroupId { get; set; }
public virtual Group Group { get; set; }
}
public class Group
{
public int GroupId { get; set; }
public string Name { get; set; }
}
这是创建代码
var db = new QuanLySinhVien();
var sinhvien = new SinhVien { Name = "Test Name", Address="Address", BirthDay=DateTime.Now };
db.SinhViens.Add(sinhvien);
db.SaveChanges();
这是消息
System.Data.Entity.ModelConfiguration.ModelValidationException:在模型生成期间检测到一个或多个验证错误:
demoMVVM.SinhVien :: EntityType'SinhVien'没有定义键。 定义此EntityType的键。
SinhViens:EntityType:EntitySet'SinhViens'基于类型'SinhVien',没有定义键。在System.Data.Entity.Core.Metadata.Edm.EdmModel.Validate()
在System.Data.Entity.DbModelBuilder.Build(DbProviderManifest providerManifest,DbProviderInfo providerInfo)
在System.Data.Entity.DbModelBuilder.Build(DbConnection providerConnection)
在System.Data.Entity.Internal.LazyInternalContext.CreateModel(LazyInternalContext internalContext)
在System.Data.Entity.Internal.RetryLazy2.GetValue(TInput input)
at System.Data.Entity.Internal.LazyInternalContext.InitializeContext()
at System.Data.Entity.Internal.InternalContext.Initialize()
at System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType)
at System.Data.Entity.Internal.Linq.InternalSet2.GetValue(TInput input)
at System.Data.Entity.Internal.LazyInternalContext.InitializeContext()
at System.Data.Entity.Internal.InternalContext.Initialize()
at System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType)
at System.Data.Entity.Internal.Linq.InternalSet2.GetValue(TInput input)
1.Initialize()
at System.Data.Entity.Internal.LazyInternalContext.InitializeContext()
at System.Data.Entity.Internal.InternalContext.Initialize()
at System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType)
at System.Data.Entity.Internal.Linq.InternalSet
在System.Data.Entity.Internal.Linq.InternalSet1.get_InternalContext()
at System.Data.Entity.Internal.Linq.InternalSet1.get_InternalContext()
at System.Data.Entity.Internal.Linq.InternalSet1.get_InternalContext()
1.ActOnSet(Action action,EntityState newState,Object entity,String methodName)
at System.Data.Entity.Internal.Linq.InternalSet
at System.Data.Entity.Internal.Linq.InternalSet1.Add(Object entity)
at System.Data.Entity.DbSet1.Add(Object entity)
at System.Data.Entity.DbSet1.Add(Object entity)
1.Add(TEntity实体)
at System.Data.Entity.DbSet
在d:\\ LuuTru \\ CSharp \\ WPF Application \\ TestMVVM \\ demoMVVM \\ MainWindow.xaml.cs中的demoMVVM.MainWindow.btnAdd_Click(Object sender,RoutedEventArgs e):第48行
导入System.ComponentModel.DataAnnotations
更改您的模型如下:
public class SinhVien
{
[Key]
public int SinhVienId { get; set; }
public string Name { get; set; }
public string Address { get; set; }
public DateTime BirthDay { get; set; }
public int GroupId { get; set; }
[ForeignKey("GroupId")]
public virtual Group Group { get; set; }
}
public class Group
{
[Key]
public int GroupId { get; set; }
public string Name { get; set; }
}
您得到了例外,因为您尚未为模型定义primary key
。 默认情况下,EF将Id
属性作为primary key
,但如果您为键使用其他名称,则必须明确delcare它(在您的情况下是SinhVienId
和GroupId
UPDATE
您可以在app.config / web.config中的connectionString
定义数据库名称,然后将connectionString
名称传递给DbContext
构造函数,如下所示:
<add
name="connectionStringName"
connectionString="Data Source=|DataDirectory|\yourdbfile.mdf"
providerName="System.Data.SqlClient" />
和DbContext
构造函数:
public class ModelContext : DbContext
{
public ModelContext()
: base("connectionStringName")
{
}
}
请注意|DataDirectory|
将指向您的App_Data
文件夹
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.