Entity Framework 4.1Code First 连接到 Sql Server 2005

发布于 2024-10-24 03:06:52 字数 1514 浏览 1 评论 0原文

我正在尝试将 Entity Framework 4.1 RC 与 SQL Server 2005 实例一起使用。我创建了一个空数据库,我想将 POCO 对象保存到其中。我的 POCO 看起来像:

public class Cart
{
    public Cart()
    {
        this.CartId = Guid.NewGuid();

    }

    public Guid CartId { get; set; }
    public decimal TotalCost { get; set; }
    public decimal SubTotalCost { get; set; }
    public decimal Tax { get; set; }
    public decimal EstimatedShippingCost { get; set; }
}

我的 CartContext 是:

public class CartContext : DbContext
{
    public DbSet<Cart> Carts { get; set; }
    public DbSet<Attribute> Attributes { get; set; }
    public DbSet<AttributeItem> AttributeItems { get; set; }
}

我有一个连接字符串:

<add name="CartContext" connectionString="Server=myserver.mynetwork.net;User ID=MyUser;Pwd=mypassword;Initial Catalog=ExistingDb" providerName="System.Data.SqlClient" \>

当我尝试将对象添加到上下文并保存它时,我得到:

System.Data.Entity.Infrastruct.DbUpdateException: 更新时发生错误 条目。请参阅内部异常 细节。 ---> System.Data.UpdateException:错误 更新条目时发生。 有关详细信息,请参阅内部异常。 ---> System.Data.SqlClient.SqlException: 对象名称“dbo.Carts”无效。

如果我分析数据库,我可以看到用户连接,在 sys.tables 中查找数据库,运行此查询:

SELECT TOP (1) 
[Extent1].[Id] AS [Id], 
[Extent1].[ModelHash] AS [ModelHash]
FROM [dbo].[EdmMetadata] AS [Extent1]
ORDER BY [Extent1].[Id] DESC 

然后尝试插入我的购物车对象。它从不尝试创建 Carts 表。我猜连接字符串有问题,但我无法在任何地方找到有关如何执行此操作的示例。

I'm trying to use the Entity Framework 4.1 RC with a SQL Server 2005 instance. I've created an empty database and I'd like to persist my POCO objects to it. My POCO looks like:

public class Cart
{
    public Cart()
    {
        this.CartId = Guid.NewGuid();

    }

    public Guid CartId { get; set; }
    public decimal TotalCost { get; set; }
    public decimal SubTotalCost { get; set; }
    public decimal Tax { get; set; }
    public decimal EstimatedShippingCost { get; set; }
}

My CartContext is:

public class CartContext : DbContext
{
    public DbSet<Cart> Carts { get; set; }
    public DbSet<Attribute> Attributes { get; set; }
    public DbSet<AttributeItem> AttributeItems { get; set; }
}

I have a connection string:

<add name="CartContext" connectionString="Server=myserver.mynetwork.net;User ID=MyUser;Pwd=mypassword;Initial Catalog=ExistingDb" providerName="System.Data.SqlClient" \>

When I try and add an object to the context and save it I get:

System.Data.Entity.Infrastructure.DbUpdateException:
An error occurred while updating the
entries. See the inner exception for
details. --->
System.Data.UpdateException: An error
occurred while updating the entries.
See the inner exception for details.
---> System.Data.SqlClient.SqlException:
Invalid object name 'dbo.Carts'.

If I profile the database I can see the user connect, look for the database in sys.tables run this query:

SELECT TOP (1) 
[Extent1].[Id] AS [Id], 
[Extent1].[ModelHash] AS [ModelHash]
FROM [dbo].[EdmMetadata] AS [Extent1]
ORDER BY [Extent1].[Id] DESC 

Then attempt to insert my cart object. It never tries to create the Carts table. I'm guessing there's something wrong with the connection string, but I can't find examples anywhere on how to do this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

吝吻 2024-10-31 03:06:52

DbContext 不会因为表不存在而创建它。使用现有数据库后,您还必须手动创建表或创建自定义初始值设定项。默认初始化程序只能删除数据库并使用所有必需的表创建新数据库。

例如,您可以致电:

context.Database.Delete();
context.Database.Create();

或:

context.Database.CreateIfNotExists();

或:

Database.SetInitializer(new DropCreateDatabaseIfModelChanges<MyContext>());
// You don't need to call this. Initialization takes place anyway if context 
// needs it but you can enforce initialization for example in the application 
// startup instead of before the first database operation
context.Database.Initialize(true); 

DbContext will not create table just because it doesn't exists. Once you are using existing database you must also manually create tables or create custom initializer. Default initializers are only able to drop the database and create new one with all required tables.

You can for example call:

context.Database.Delete();
context.Database.Create();

Or:

context.Database.CreateIfNotExists();

Or:

Database.SetInitializer(new DropCreateDatabaseIfModelChanges<MyContext>());
// You don't need to call this. Initialization takes place anyway if context 
// needs it but you can enforce initialization for example in the application 
// startup instead of before the first database operation
context.Database.Initialize(true); 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文