繁体   English   中英

无法使用Entity Framework代码创建数据库 - 首先+没有数据库

[英]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.RetryLazy 2.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.InternalSet
2.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.InternalSet
2.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.InternalSet
1.Initialize()

在System.Data.Entity.Internal.Linq.InternalSet 1.get_InternalContext()
at System.Data.Entity.Internal.Linq.InternalSet
1.get_InternalContext()
at System.Data.Entity.Internal.Linq.InternalSet
1.get_InternalContext()
at System.Data.Entity.Internal.Linq.InternalSet
1.ActOnSet(Action action,EntityState newState,Object entity,String methodName)

at System.Data.Entity.Internal.Linq.InternalSet 1.Add(Object entity)
at System.Data.Entity.DbSet
1.Add(Object entity)
at System.Data.Entity.DbSet
1.Add(Object entity)
at System.Data.Entity.DbSet
1.Add(TEntity实体)

在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它(在您的情况下是SinhVienIdGroupId

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM