实体框架EntityKey/外键问题
我不断收到相同的错误:“VlaamseOverheidMeterEntities.ObjectMeter”中的实体参与“FK_ObjectMeter_Meter”关系。找到 0 条相关“仪表”。预计为 1“米”。
我有以下表结构:
Meter 1 <- * ObjectMeter * -> 1 VO_Object
始终是相同的情况:第一个仪表被添加到数据库中,第二个仪表给出上述错误。
我的经理中有以下代码:
public List<string> addTemporary(string username, string meterNaam, string readingType, string parentID) {
Meter meter = new Meter();
VO_Object voObject = objectManager.getObjectByID(parentID);
ObjectMeter objMeter = new ObjectMeter();
meter.readingType = (int)Enum.Parse(typeof(ReadingType), readingType);
meter.isActive = true;
meter.name = meterNaam;
meter.startDate = DateTime.Now;
meter.endDate = DateTime.Now.AddYears(6000);
meter.uniqueIdentifier = "N/A";
meter.meterType = (int)Enum.Parse(typeof(MeterType), "NA");
meter.meterCategory = (int)Enum.Parse(typeof(MeterCategory), "NA");
meter.energyType = (int)Enum.Parse(typeof(EnergyType), "NA");
meter.utilityType = (int)Enum.Parse(typeof(UtilityType), "NA");
meter.unitOfMeasure = (int)Enum.Parse(typeof(UnitOfMeasure), "NA");
objMeter.valid_from = meter.startDate;
objMeter.valid_until = meter.endDate;
objMeter.Meter = meter;
objMeter.VO_Object = voObject;
createMeter(meter);
List<String> str = new List<string>();
str.Add("" + meter.meterID);
str.Add(meter.name);
return str;
}
在链接到数据库的 Dao 类中有以下代码:
internal void CreateMeter(Meter _meter) {
_entities.AddToMeter(_meter);
_entities.SaveChanges();
}
有人可以解释一下这个错误吗?
罗纳德
I keep getting the same error: Entities in 'VlaamseOverheidMeterEntities.ObjectMeter' participate in the 'FK_ObjectMeter_Meter' relationship. 0 related 'Meter' were found. 1 'Meter' is expected.
I have the following table structure:
Meter 1 <- * ObjectMeter * -> 1 VO_Object
It is always the same scenario: The first meter is added to the database, the second meter gives the error above.
I have the following code in my manager:
public List<string> addTemporary(string username, string meterNaam, string readingType, string parentID) {
Meter meter = new Meter();
VO_Object voObject = objectManager.getObjectByID(parentID);
ObjectMeter objMeter = new ObjectMeter();
meter.readingType = (int)Enum.Parse(typeof(ReadingType), readingType);
meter.isActive = true;
meter.name = meterNaam;
meter.startDate = DateTime.Now;
meter.endDate = DateTime.Now.AddYears(6000);
meter.uniqueIdentifier = "N/A";
meter.meterType = (int)Enum.Parse(typeof(MeterType), "NA");
meter.meterCategory = (int)Enum.Parse(typeof(MeterCategory), "NA");
meter.energyType = (int)Enum.Parse(typeof(EnergyType), "NA");
meter.utilityType = (int)Enum.Parse(typeof(UtilityType), "NA");
meter.unitOfMeasure = (int)Enum.Parse(typeof(UnitOfMeasure), "NA");
objMeter.valid_from = meter.startDate;
objMeter.valid_until = meter.endDate;
objMeter.Meter = meter;
objMeter.VO_Object = voObject;
createMeter(meter);
List<String> str = new List<string>();
str.Add("" + meter.meterID);
str.Add(meter.name);
return str;
}
and this in my Dao Class which links to the database:
internal void CreateMeter(Meter _meter) {
_entities.AddToMeter(_meter);
_entities.SaveChanges();
}
Can someone please explain this error?
Ronald
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果我理解你的代码:VO_Object 是 ObjectMeter 的父级,而 ObjectMeter 是 Meter 的父级。 VO_Object 已存在于数据库中。您应该在创建 Meter 之前创建(保存)ObjectMeter。
If I understand your code: VO_Object is parent to ObjectMeter which is parent to Meter. VO_Object already exists in the database. You should create (save) ObjectMeter before creating Meter.