将具有关系的新对象插入具有标识列的新对象
客户类别:
class Customer
{
int id;
int phone;
int fax;
PhoneNumber phone1;
PhoneNumber fax1;
}
Class PhoneNumber
{
int id;
int prefixid;
string number;
}
PhoneNumber 是由 EF4 自动生成的,我已将自动生成的名称更改为phone1 和fax1。phonenumber
id 和customer id 都是数据库中的标识列。
现在我想创建一个新客户:
var newCustomer = Customer.CreateCustomer(0, CompanyID);
PhoneNumber fax = new PhoneNumber();
PhoneNumber phone = new PhoneNumber();
fax.Customers.Add(newCustomer);
phone.Customers.Add(newCustomer);
context.Customers.AddObject(newCustomer);
context.SaveChanges();
但现在我得到:
System.Data.UpdateException: {"不能 插入明确的身份值 表“PhoneNumber”中的列时 IDENTITY_INSERT 设置为 OFF。”}
为什么 EF4 不处理相关表的标识列,因为它应该处理新实体?我怎样才能完成这项工作?
(EF4 应该在 Phones 表中创建 2 个条目,获取他们的身份并将其添加到新客户的客户行中 - 至少这是我想要做的)
我知道我可以创建 2 条电话记录,然后创建客户一条,但是
我希望在 a 中完成。 b
. 如果客户创建失败 - 我不希望电话表上有空记录...
Customer class:
class Customer
{
int id;
int phone;
int fax;
PhoneNumber phone1;
PhoneNumber fax1;
}
Class PhoneNumber
{
int id;
int prefixid;
string number;
}
The PhoneNumber was auto-Generated by EF4, I've change the auto-generated names to phone1 and fax1
Both phonenumber id and customer id are identity columns in the DB.
Now I want to create a new Customer:
var newCustomer = Customer.CreateCustomer(0, CompanyID);
PhoneNumber fax = new PhoneNumber();
PhoneNumber phone = new PhoneNumber();
fax.Customers.Add(newCustomer);
phone.Customers.Add(newCustomer);
context.Customers.AddObject(newCustomer);
context.SaveChanges();
But now I get:
System.Data.UpdateException: {"Cannot
insert explicit value for identity
column in table 'PhoneNumber' when
IDENTITY_INSERT is set to OFF."}
Why isn't EF4 deal with the identity column of the related table as it should deal with a new entity ? How can I make this work ?
(EF4 Should've create 2 entiries in Phones table, get their identity and add it to the customer row of the new customer - at least that what I want to do)
I know I Can create 2 Phone records, and than create the customer one, but
a. I want it to be done in one act.
b. if cusotmer creation fails - I don't want empty records on the phones table...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
那么这两件事是相互矛盾的:
但随后出现错误:
这个错误可能看起来像是正在设置重复的标识,但事实并非如此 - 它是在说您试图显式设置身份字段。
也就是说,PhoneNumber 中的 ID 列不是身份字段。或者至少 EF 不这么认为。
所以我有四个问题:
1)您是否确认该字段在 EDMX 中设置为 IDENTITY?也许有什么东西已经覆盖了它?
2)
CreateCustomer
方法有什么作用?它只是新建一个客户并设置一些属性吗?它不应触及身份字段。3) 您是否在 EDMX 上正确设置了导航属性/基数?例如 1 个客户 .. 1-* 电话号码。
4) 尝试将电话号码添加给客户,而不是相反。将“多”添加到“一”是有意义的 - 例如,客户是这里的聚合:
我还建议重新设计您的模型。
让客户拥有 1 个传真和 1 个电话号码,每个号码上都有一个属性,这是愚蠢的。应该是一个拥有许多“PhoneNumber”实体的客户。
您可以在 PhoneNumber 实体上使用 TPH 来区分传真和其他类型,上面的代码会更有意义:
Well these two things contradict each other:
But then the error:
It may seem like this error is saying a duplicate identity is being set, but it's not - it's saying your trying to explicitly set the identity field.
What that is saying is that the ID column in PhoneNumber is not an identity field. Or at least EF does not think it is.
So i have four questions:
1) Have you confirmed the field is set to IDENTITY in the EDMX? Maybe something has overriden it?
2) What does the
CreateCustomer
method do? Does it just new up a Customer and set some properties? It should not touch the identity fields.3) Have you setup the navigational properties/cardinalities properly on the EDMX? E.g 1 Customer .. 1-* Phone Numbers.
4) Try adding the Phone number to the Customer, not the other way around. It makes sense to add the "many" to the "one" - e.g Customer is the aggregate here:
I'd also suggest redesigning your model.
It's silly to have Customer with 1 Fax and 1 Phone Number, each with a property on there. It should be a Customer have many "PhoneNumber" entities.
You could use TPH on the PhoneNumber entity to discriminate between Fax and other types, and the above code would make much more sense: