实体框架异常:对象名称无效 [英] Entity Framework Exception: Invalid object name
问题描述
例外:
更新条目时出错。查看内部例外情况。
内部例外:
无效的对象名称'dbo.Dinners'。
注意:我在数据库中没有这样的表(Dinners),代码应该创建表,只是给出了连接字符串来标识服务器,如
LibraryReservationSystem数据库已经存在数据库。它
没有表。我期待EF创建表。
这是不正确的。如果数据库存在,EF不会在此数据库中创建任何表。 EF可以创建数据库(如果不存在)。这是默认的数据库初始化程序 CreateDatabaseIfNotExists
,如果不明确更改它将被应用。您可以选择其他两个初始值: DropCreateDatabaseAlways
或 DropCreateDatabaseIfModelChanges
。但是,这些都不会只在现有数据库中创建表,而是完全删除数据库,并从头创建,包括所有表。
你可以做什么:
- 可以手动删除数据库(例如在SSMS中),然后EF将创建一个新的数据,包括表
-
或者如果您无法删除数据库,无论如何在(错误,感谢Mark Stafford的评论)Seed
方法中写入SQL代码,将表添加到数据库 - 或使用代码优先迁移(EF> = 4.3)在现有数据库中添加新表,当您有添加新实体。
<或者使用
DropCreateDatabaseAlways
initializer 一次让EF创建包含表的数据库,然后再次删除初始化程序 I am trying to create database using Code First approach. When I run the following code I am getting following exception. Is there anything wrong in the fields that I defined? How can we overcome this?
Exception:
An error occurred while updating the entries. See the inner exception for details.
Inner Exception:
"Invalid object name 'dbo.Dinners'.
Note: I do not have such a table (Dinners) in databse. The code is supposed to create the tables. I just gave connection string to identify the server as mentioned in EF Code First: Cannot connect to SQL Server. Should I change the connection string?
Connections String:
string connectionstring = "Data Source=.;Initial Catalog=LibraryReservationSystem;Integrated Security=True;Connect Timeout=30";
The LibraryReservationSystem database is already existing database. It has no tables. I am expecting EF to create the tables.
The connection string I copied from a working LINQ 2 SQL application. Do I need to make any changes on it to supply to EF?
UPDATE
When I included the following code, the exception got changed. Now it says - "Invalid object name 'dbo.Dinner'.". It is now complaining about Dinner table; not Dinners table.
protected override void OnModelCreating(DbModelBuilder modelbuilder)
{
modelbuilder.Conventions.Remove<PluralizingTableNameConvention>();
}
Original CODE
static void Main(string[] args)
{
string connectionstring = "Data Source=.;Initial Catalog=LibraryReservationSystem;Integrated Security=True;Connect Timeout=30";
using (var db = new NerdDinners(connectionstring))
{
var product = new Dinner { DinnerID = 1, Title = 101 };
db.Dinners.Add(product);
int recordsAffected = db.SaveChanges();
}
}
using System.Data.Entity;
namespace LijosEF
{
public class Dinner
{
public int DinnerID { get; set; }
public int Title { get; set; }
}
public class RSVP
{
public int RSVPID { get; set; }
public int DinnerID { get; set; }
public virtual Dinner Dinner { get; set; }
}
//System.Data.Entity.DbContext is from EntityFramework.dll
public class NerdDinners : System.Data.Entity.DbContext
{
public NerdDinners(string connString): base(connString)
{
}
public DbSet<Dinner> Dinners { get; set; }
public DbSet<RSVP> RSVPs { get; set; }
}
}
REFERENCE
- http://nerddinner.codeplex.com/discussions/358197
- Entity framework - Invalid Object Name
- Invalid object name 'dbo.TableName' when retrieving data from generated table
- http://blogs.msdn.com/b/adonet/archive/2011/09/28/ef-4-2-code-first-walkthrough.aspx
The LibraryReservationSystem database is already existing database. It has no tables. I am expecting EF to create the tables.
That's not correct. If the database exists EF doesn't create any tables in this database. EF can create the database if it doesn't exist. That is the default database initializer CreateDatabaseIfNotExists
that gets applied if you don't change it explicitly. You can select two other initializers: DropCreateDatabaseAlways
or DropCreateDatabaseIfModelChanges
. But neither of those will only create tables in an existing database but instead delete the database completely and create it from scratch including all tables.
What can you do:
- Either delete the database manually (in SSMS for example), then EF will create a new one including the tables
- Or use the
DropCreateDatabaseAlways
initializer once to let EF create the database including the tables, then remove the initializer again Or if you can't delete the database for whatever reason write SQL code in the(Wrong, thanks to Mark Stafford's comment)Seed
method that adds the tables to the database- Or use Code-First Migrations (EF >= 4.3) to add new tables to an existing database when you have added new entities.
这篇关于实体框架异常:对象名称无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!