如何在服务器端创建并存储一个(自跟踪)实体对象?

发布于 2024-09-12 23:48:35 字数 1480 浏览 10 评论 0原文

我正在尝试使用实体框架 4.0 和自跟踪实体来实现以下目标:

1)客户端应用程序通过提供 ISBN 编号向服务器请求一本书

2)服务器对其数据库执行查询以查看该书是否已经存在 。

3a) 如果该书在数据库中,则返回该书

3b)如果数据库中没有这本书,它将查询亚马逊信息,提取所需的属性,创建一本新书,将其存储在数据库中,然后将其返回给客户端

现在,3b)是问题所在。 ..我找不到任何有关如何在服务器端创建实体对象(一本书)、将其添加到上下文并将其存储在数据库中的信息。我尝试过各种各样的事情:

public class BookBrowserService : IBookBrowserService {
    public Book GetBook(string ISBN) {
        using (var ctx = new BookBrowserModelContainer()) {
            Book book = ctx.Books.Where(b => b.ISBN == ISBN).SingleOrDefault();
            if (book == null) {
                book = new Book();
                book.ISBN = ISBN; // This is the key
                book.Title = "This title would be retrieved from Amazon";

                Author author = new Author();
                author.Name = "The author's name would be retrieved from Amazon";
                book.Authors.Add(author);

                ctx.Books.AddObject(book);
                ctx.SaveChanges(); // This one always throws an exception...
            }
            return book;
        }
    }
}

谁能告诉我我做错了什么? 看起来问题与 EDMX 模型有关。

我有一个 Book 实体和一个 Author 实体,具有多对多关系。

Book实体的Key是ISBN,它是一个最大长度为13的字符串。 StoreGeneratePattern 设置为“无”。

作者实体的Key是Id,它是一个Guid。 StoreGeneratePattern 是身份。

异常消息是: “无法将 NULL 值插入表 'BookBrowser.dbo.Authors' 列 'Id';列不允许为空值。INSERT 失败。该语句已终止。”

但由于 StoreGeneratePattern 设置为 Identity,因此不应该有 Id自动创造价值?

谢谢, 彼得

I am trying to achieve the following using Entity framework 4.0 and self-tracking entities:

1) The client application request a book form the server by providing an ISBN number

2) The server performs a query on its database to see if the book is already present

3a) If the book is in the database, it returns it.

3b) If the book is not in the database, it will query Amazon for info, extract the required attributes, create a new book, store it in the database, and return it to the client

Now, 3b) is where the problems are... I can't find any information on how I can create an entity object (a book) on the server side, add it to the context and store it in the database. I have tried all sorts of things:

public class BookBrowserService : IBookBrowserService {
    public Book GetBook(string ISBN) {
        using (var ctx = new BookBrowserModelContainer()) {
            Book book = ctx.Books.Where(b => b.ISBN == ISBN).SingleOrDefault();
            if (book == null) {
                book = new Book();
                book.ISBN = ISBN; // This is the key
                book.Title = "This title would be retrieved from Amazon";

                Author author = new Author();
                author.Name = "The author's name would be retrieved from Amazon";
                book.Authors.Add(author);

                ctx.Books.AddObject(book);
                ctx.SaveChanges(); // This one always throws an exception...
            }
            return book;
        }
    }
}

Could anyone tell me what I am doing wrong?
It looks like the problem is related to the EDMX model.

I have a Book entity and an Author entity, with a many-to-many relationship.

The Book entity's Key is ISBN, which is a string of Max length 13.
StoreGeneratedPattern is set to None.

The Author entity's Key is Id, which is a Guid.
StoreGeneratedPattern is Identity.

The exception message is:
"Cannot insert the value NULL into column 'Id', table 'BookBrowser.dbo.Authors'; column does not allow nulls. INSERT fails. The statement has been terminated. "

But since StoreGeneratedPattern is set to Identity, shouldn't an Id value be created automatically?

Thanks,
Peter

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

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

发布评论

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

评论(2

季末如歌 2024-09-19 23:48:35

看来问题是我使用 Guid 作为 Key 与 StoreGeneratePattern = Identity 结合使用。

当我将 StoreGeneratePattern 设置为 None 并使用 Id = Guid.NewGuid() 创建自己的 Guid 时,问题就消失了。

显然,SQL 服务器无法生成 Guid...

It looks that the problem was that I used a Guid as Key in combination with StoreGeneratedPattern = Identity.

When I set StoreGeneratedPattern to None and create my own Guid using Id = Guid.NewGuid(), the problem is gone.

Apparently, the SQL server cannot generate Guids...

心碎的声音 2024-09-19 23:48:35

您可以使用 StoreGeneratePattern=Identity,但是根据您的 edmx 生成的 sql 脚本在描述主键(GUID)时不包含 newid() 。您可以在生成的 sql 脚本中手动执行此操作。 'BookId 唯一标识符 NOT NULL
默认 newid()'。所以id值会自动创建GUID。

you can use StoreGeneratedPattern=Identity, but generated sql script based on your edmx doesn`t contain newid() in describing primary key(GUID). you can do this manually in generated sql script. 'BookId uniqueidentifier NOT NULL
DEFAULT newid()'. So id value will create GUID automatically.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文