问题
I have a model as shown below (created using Model-First approach).
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.
"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."}
How to overcome this?
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?
Note: When I try to make the field "PurchasePurchaseId" nullable in model, I am getting following error:
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
CODE
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();
}
}
}
REFERENCE:
Database design - articles, blog posts, photos, stories
Can I get Entity Framework (model-first) to generate composite keys?
回答1:
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. shouldn't you use many-to-many relation instead because selling item can be sold multiple times and purchase can have multiple selling items?
来源:https://stackoverflow.com/questions/11686727/adding-reference-to-entity-causing-exception