实体框架:插入与已经知道外键的对象,但在数据库尚不存在 [英] Entity Framework: insert an object with a foreign key which is already known, but doesn't exist in DB yet

查看:104
本文介绍了实体框架:插入与已经知道外键的对象,但在数据库尚不存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用实体框架,并想将对象插入到该引用不存在于数据库中存在另一个对象的数据库(但外键是已知的)。

I'm using Entity Framework and would like to insert an object into the database that references another object that doesn't exist in the DB yet (but the foreign key is known already).

简单的例子:假设我有两个表,图书(其ISBN标识)和购买。我希望能够插入购买行为,即使这本书尚未在DB设置尚未:

Simple example: assume I have two tables, Books (identified by their ISBN) and Purchases. I'd like to be able to insert purchases, even if the book hasn't been set up in the DB yet:

public class Book
{
    [Key]
    public virtual string ISBN { get; set; }

    protected virtual IEnumerable<Purchase> purchases { get; set; }

    public virtual string author { get; set; }
    public virtual string title { get; set; }
// .....
}

public class Purchase
{
    [Key]
    protected virtual int id { get; set; }

    public virtual Book book { get; set; }

    public virtual double price;
}



从概念上讲,这很容易:在购买表将有一个外键,它是本书的ISBN。因此,在常规的SQL,我可以插入ISBN进入这个领域,这本书添加到books表晚些时候。

Conceptually, this would be easy: the Purchase table will have a foreign key that is the book's ISBN. So in regular SQL, I could just insert the ISBN into this field, and add the book to the books table sometime later.

在EF / C#不过,我可以' ŧ想到一个简单的方法来做到这一点。在购买类引用图书类,所以我不能只使用ID(ISBN)直接在这里。

In EF/C# though, I can't think of an easy way to do this. The Purchase class references the Book class, so I can't just use the ID (ISBN) directly here.

我能做的就是创建一个虚拟的书对象,并用它来插入选购。问题是,实体框架自动插入虚拟书的对象插入表中也一样,这是我想避免:

What I could do is create a dummy book object and use this to insert the purchase. Problem is that Entity Framework automatically inserts the dummy book object into the table too, which I want to avoid:

var dummyBook = new Book();
dummyBook.isbn = "978-0141354217";

var purchase = new Purchase();
purchase.price = 99.95;
purchase.book = dummyBook;

dbContext.Purchases.Add(purchase);

// Should add the purchase to the DB, but NOT the book!
// Unfortunately, EF adds both automatically...
dbContext.SaveChanges();



有没有办法做到这一点?

Is there any way to do this?

谢谢!

推荐答案

尽管书号是(应该是)独特的,似乎是一个很好的候选人一个主键,这是从来没有使用任何自然键作为数据库中的主键一个很好的做法。

Despite the fact that ISBN is (should be) unique and seems like a good candidate for a primary key, it's never a good practice to use any natural key as a primary key in the database.

详细位置: HTTP://en.wikipedia .ORG /维基/ Natural_key

所以,一旦ISBN是不是你的主键,而只是一个字符串字段,你就可以做什么你想:插入特定ISBN购买,而不必本书的记录。在这种情况下,你不会有购买类的虚拟图书属性。你可以用这样的替换它:

So, once the ISBN is not your primary key, but just a string field, you'll be able to do what you want: insert a purchase for a particular ISBN without having the book record. In this case you wouldn't have virtual Book property of the Purchase class. You could replace it with a something like this:

public Book GetBook (string isbn, DbContext context)
{
    return context.Books.Where(x => x.ISBN == isbn).FirstOrDefaul();
}



这一切都没有进入讨论为什么你就必须购买一本书这并不在DB存在

All this without going into discussion why you'd have a purchase of a book that doesn't exist in the DB.

这篇关于实体框架:插入与已经知道外键的对象,但在数据库尚不存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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