使用EF Code First创建数据库时出现无效的对象名称错误 [英] Invalid Object Name error when creating database with EF Code First
问题描述
您好,我正在使用 EF Core Code First
开发数据库。生成数据库时,我总是收到此错误消息:
Hello i am developing a database using EF Core Code First
.I keep getting this error when generating the database :
Microsoft.EntityFrameworkCore.DbUpdateException:'在更新条目时,
发生了错误。有关详细信息,请参见内部异常。'
Microsoft.EntityFrameworkCore.DbUpdateException: 'An error occurred while updating the entries. See the inner exception for details.'
- InnerException {无效的对象名称父母。} System.Exception {Microsoft.Data。 SqlClient.SqlException}
从我阅读的内容来看,我似乎无法创建数据库,因为表以某种方式复数。
我已经在搜索解决方案,包括 SO线程和尝试解决方案无济于事。我没有尝试过的方法是 RemovePluralization
方法,因为
我在 Conventions 字段中没有此字段我的 ModelBuilder
,但是我找不到包含它的软件包。
From what i have read it seems i can not create the database because the tables are somehow pluralized.
I have searched for solutions including this SO thread and tried the solutions to no avail. The one that i have not tried is the RemovePluralization
method because
I do not have this Conventions
field in my ModelBuilder
and i can not find the package containing it.
上下文和模型
public class Parent {
public int ID { get; set; }
public string Name { get; set; }
public ICollection<Child> Children { get; set; }
public Parent() {
this.Children = new List<Child>();
}
}
public class Child {
public int ID { get; set; }
public string Name { get; set; }
public Parent Parent { get; set; }
public int ParentId { get; set; }
}
public class MyContext : DbContext {
public virtual DbSet<Parent> Parents { get; set; }
public virtual DbSet<Child> Children { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder) {
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<Child>()
.HasKey(x => x.ID);
modelBuilder.Entity<Parent>()
.HasKey(x => x.ID);
modelBuilder.Entity<Parent>()
.HasMany(x => x.Children)
.WithOne(k=>k.Parent)
.HasForeignKey(t => t.ParentId);
}
public MyContext(DbContextOptions options):base(options) {
this.Database.EnsureCreated();
this.Database.Migrate(); //i have tried this too
}
}
用法
var optionsBuilder = new DbContextOptionsBuilder<MyContext>();
optionsBuilder.UseSqlServer(connectionString);
MyContext context = new MyContext(optionsBuilder.Options);
Parent parent = new Parent() { Name = "Parentino" };
context.Parents.Add(parent);
await context.SaveChangesAsync(); //crashes with before mentioned error
我也试图在<$ c中设置表名$ c> OnModelCreating 重载了这一点:
I have also tried to set the table names in my OnModelCreating
overload this:
modelBuilder.Entity<Parent>().ToTable("parent");
或直接与 [Table( name)]
我的模型类的属性无济于事。
or directly with [Table("name")]
attribute over my Model classes to no avail.
有人可以帮我一下,告诉我为什么我不能生成数据库吗?
Could someone help me out and tell me why i can't generate the database ?
推荐答案
对我来说很好
program.sc
program.sc
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace ConsoleApp1
{
class Program
{
static async Task Main(string[] args)
{
string connectionString = "Server=(localdb)\\MSSQLLocalDB;Initial Catalog=test;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=True;ApplicationIntent=ReadWrite;MultiSubnetFailover=False";
var optionsBuilder = new DbContextOptionsBuilder<MyContext>();
optionsBuilder.UseSqlServer(connectionString);
MyContext context = new MyContext(optionsBuilder.Options);
Parent parent = new Parent() { Name = "Parentino" };
context.Parents.Add(parent);
await context.SaveChangesAsync(); //crashes with before mentioned error }
}
}
public class Parent
{
public int ID { get; set; }
public string Name { get; set; }
public ICollection<Child> Children { get; set; }
public Parent()
{
this.Children = new List<Child>();
}
}
public class Child
{
public int ID { get; set; }
public string Name { get; set; }
public Parent Parent { get; set; }
public int ParentId { get; set; }
}
public class MyContext : DbContext
{
public virtual DbSet<Parent> Parents { get; set; }
public virtual DbSet<Child> Children { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<Child>()
.HasKey(x => x.ID);
modelBuilder.Entity<Parent>()
.HasKey(x => x.ID);
modelBuilder.Entity<Parent>()
.HasMany(x => x.Children)
.WithOne(k => k.Parent)
.HasForeignKey(t => t.ParentId);
}
public MyContext(DbContextOptions options) : base(options)
{
this.Database.EnsureCreated();
this.Database.Migrate(); //i have tried this too
}
}
}
ConsoleApp1.csproj
ConsoleApp1.csproj
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.1.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="3.1.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
</Project>
这篇关于使用EF Code First创建数据库时出现无效的对象名称错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!