实体框架异常:对象名称无效 [英] Entity Framework Exception: Invalid object name

查看:115
本文介绍了实体框架异常:对象名称无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Code First方法创建数据库。当我运行以下代码我得到以下异常。我定义的字段有什么问题吗?



例外


更新条目时出错。查看内部例外情况。


内部例外


无效的对象名称'dbo.Dinners'。


注意:我在数据库中没有这样的表(Dinners),代码应该创建表,只是给出了连接字符串来标识服务器,如

  • 实体框架 - 无效的对象名称

  • 从生成的表检索数据时,无效的对象名称dbo.TableName

  • http://blogs.msdn.com/ b / adonet / archive / 2011/09/28 / ef-4-2-code-first-walkthrough.aspx


  • 解决方案


    LibraryReservationSystem数据库已经存在数据库。它
    没有表。我期待EF创建表。


    这是不正确的。如果数据库存在,EF不会在此数据库中创建任何表。 EF可以创建数据库(如果不存在)。这是默认的数据库初始化程序 CreateDatabaseIfNotExists ,如果不明确更改它将被应用。您可以选择其他两个初始值: DropCreateDatabaseAlways DropCreateDatabaseIfModelChanges 。但是,这些都不会只在现有数据库中创建表,而是完全删除数据库,并从头创建,包括所有表。



    你可以做什么:




    • 可以手动删除数据库(例如在SSMS中),然后EF将创建一个新的数据,包括表

    • <或者使用 DropCreateDatabaseAlways initializer 一次让EF创建包含表的数据库,然后再次删除初始化程序
    • 或者如果您无法删除数据库,无论如何在 Seed 方法中写入SQL代码,将表添加到数据库 (错误,感谢Mark Stafford的评论)

    • 或使用代码优先迁移(EF> = 4.3)在现有数据库中添加新表,当您有添加新实体。


    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

    1. http://nerddinner.codeplex.com/discussions/358197
    2. Entity framework - Invalid Object Name
    3. Invalid object name 'dbo.TableName' when retrieving data from generated table
    4. 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 Seed method that adds the tables to the database (Wrong, thanks to Mark Stafford's comment)
    • Or use Code-First Migrations (EF >= 4.3) to add new tables to an existing database when you have added new entities.

    这篇关于实体框架异常:对象名称无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆