实体框架EntityKey/外键问题

发布于 2024-09-03 10:04:59 字数 1738 浏览 3 评论 0原文

我不断收到相同的错误:“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

半边脸i 2024-09-10 10:04:59

如果我理解你的代码: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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文