添加引用异常的实体的引用 [英] Adding reference to entity causing Exception

查看:165
本文介绍了添加引用异常的实体的引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个如下所示的模型(使用 Model-First 方法创建)。

I have a model as shown below (created using Model-First approach).

一本书是一个销售项目。 DigitalDisc是一个销售项目。这是正常工作,数据正确插入数据库。

A book is a selling item. A DigitalDisc is a selling item. This is working fine and data is getting inserted correctly in database.

我正在添加一个名为购买的新实体。一个购买包含多个销售项目。一旦我添加了这个新关联并更新了数据库模式,我得到以下异常。

I am adding a new entity named "Purchase". One purchase contains multiple sellingitems. Once I added this new association and updated the database schema, I am getting the following exception.


更新条目时出错。

An error occurred while updating the entries.

INSERT语句冲突使用FOREIGN KEY约束\FK_PurchaseSellingItem\。冲突发生在数据库\LibraryReservationSystem\中,表\dbo.Purchases\,PurchaseId列。\r \\已被终止。}

"The INSERT statement conflicted with the FOREIGN KEY constraint \"FK_PurchaseSellingItem\". The conflict occurred in database \"LibraryReservationSystem\", table \"dbo.Purchases\", column 'PurchaseId'.\r\nThe statement has been terminated."}

如何克服这个问题?

注意:我需要创建一本书(像将数据输入数据库)。本书可能会或不会发生购买。当这本书被创建时,这本书的外键列应为空。但是当该列被更新时,它应该是在Purchase表中存在的ID是可能的?

Note: I need to create a book (like entering book data into database). The purchase may or may not happen for this book. When the book is created the foreign key column should be null for that book. But when that column is updated, it should be an ID present in Purchase table Is that possible?

注意:当我尝试使字段PurchasePurchaseId在模型中为空时,我正在追踪错误:

Note: When I try to make the field "PurchasePurchaseId" nullable in model, I am getting following error:


错误1错误113:在PurchaseSellingItem关系中角色购买中的多重性无效。因为从属角色中的所有属性都为空,所以主体角色的多重性必须为0..1。 C:\Documents and Settings\U16990\My Documents\Visual Studio 2010\Projects\LijosEF\LijosEF\MyModelFirstTest.edmx 35 3 LijosEF

Error 1 Error 113: Multiplicity is not valid in Role 'Purchase' in relationship 'PurchaseSellingItem'. Because all the properties in the Dependent Role are nullable, multiplicity of the Principal Role must be '0..1'. C:\Documents and Settings\U16990\My Documents\Visual Studio 2010\Projects\LijosEF\LijosEF\MyModelFirstTest.edmx 35 3 LijosEF

代码

class Program
{

    static string connectionStringVal;

    static void Main(string[] args)
    {

        SqlConnectionStringBuilder sqlBuilder = new SqlConnectionStringBuilder();
        sqlBuilder.DataSource = ".";
        sqlBuilder.InitialCatalog = "LibraryReservationSystem";
        sqlBuilder.IntegratedSecurity = true;

        // Initialize the EntityConnectionStringBuilder.
        EntityConnectionStringBuilder entityBuilder = new EntityConnectionStringBuilder();
        entityBuilder.Provider = "System.Data.SqlClient";
        entityBuilder.ProviderConnectionString = sqlBuilder.ToString();
        entityBuilder.Metadata = @"res://*/MyModelFirstTest.csdl|res://*/MyModelFirstTest.ssdl|res://*/MyModelFirstTest.msl";

        connectionStringVal = entityBuilder.ToString();


        AddBook();
        AddDisc();





    }

    private static void AddBook()
    {

        //string connectionstring = "Data Source=.;Initial Catalog=SalesDB;Integrated Security=True;Connect Timeout=30";
        using (var db = new MyModelFirstTestContainer(connectionStringVal))
        {
            Book book = new Book();
            book.AvailabilityStatus = "Available";
            book.Price = 150;
            book.Title = "Maths Easy";

            db.SellingItems.AddObject(book);
            db.SaveChanges();

        }

    }

    private static void AddDisc()
    {

        //string connectionstring = "Data Source=.;Initial Catalog=SalesDB;Integrated Security=True;Connect Timeout=30";
        using (var db = new MyModelFirstTestContainer(connectionStringVal))
        {
            DigitalDisc disc = new DigitalDisc();
            disc.AvailabilityStatus = "Available";
            disc.Price = 300;
            disc.Artist = "Turtle Violin";

            db.SellingItems.AddObject(disc);
            db.SaveChanges();

        }

    }

}

参考:


  1. 数据库设计 - 文章,博文,照片,故事

我可以获得Entity Framework(最先创建)来生成复合密钥吗?


推荐答案

您将关系配置为强制性,但您正在保存 SellingItem 没有相关的购买。我会期望EF将保存记录到数据库之前会抛出一些异常,但它没有,它最有可能尝试传递FK列的默认值,而您没有任何 Purchase 这样的 Id

You configured your relation to be mandatory but you are saving SellingItem without related Purchase. I would expect that EF will throw some exception before saving records to the database but it didn't and it most probably tries to pass default value for FK column and you don't have any Purchase with such Id.

Btw。不应该使用多对多的关系,因为销售物品可以多次出售,购买可以有多个销售物品?

Btw. shouldn't you use many-to-many relation instead because selling item can be sold multiple times and purchase can have multiple selling items?

这篇关于添加引用异常的实体的引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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