EntityReference 的 EntityKey 属性值不匹配?

发布于 2024-08-04 11:19:03 字数 5553 浏览 3 评论 0原文

我正在尝试添加一些我创建的实体。当我尝试将有问题的实体添加到集合中时(请参阅下面的代码),我收到以下错误:

“无法添加或附加该对象,因为其 EntityReference 的 EntityKey 属性值与该对象的 EntityKey 不匹配。 ”

但我不知道它指的是哪个实体键。这是代码,可能还有更好的方法来解决这个问题:

public Internship CreateInternship(Internship internshipToCreate)
{
    try
    {
        Contact contactToCreate = new Contact();

        contactToCreate.Fax = internshipToCreate.contacts.Fax;
        contactToCreate.Extension = internshipToCreate.contacts.Extension;
        contactToCreate.FirstName = internshipToCreate.contacts.FirstName;
        contactToCreate.MiddleName = internshipToCreate.contacts.MiddleName;
        contactToCreate.LastName = internshipToCreate.contacts.LastName;
        contactToCreate.PhoneNumber = internshipToCreate.contacts.PhoneNumber;
        contactToCreate.StreetAddress = internshipToCreate.contacts.StreetAddress;
        contactToCreate.PostalCode = internshipToCreate.contacts.PostalCode;
        contactToCreate.ContactEmail = internshipToCreate.contacts.ContactEmail;

        contactToCreate.statesReference.EntityKey =
                    new EntityKey("InternshipEntities.StateSet", "ID", internshipToCreate.contacts.states.ID);
        contactToCreate.countriesReference.EntityKey =
                    new EntityKey("InternshipEntities.CountrySet", "ID", internshipToCreate.contacts.countries.ID);

        _internshipEntities.AddToContactSet(contactToCreate);
        _internshipEntities.SaveChanges();

        try
        {
            Availability availabilityToCreate = new Availability();

            availabilityToCreate.StartDate = internshipToCreate.availability.StartDate;
            availabilityToCreate.EndDate = internshipToCreate.availability.EndDate;
            availabilityToCreate.Negotiable = internshipToCreate.availability.Negotiable;

            _internshipEntities.AddToAvailabilitySet(availabilityToCreate);
            _internshipEntities.SaveChanges();

            try
            {
                internshipToCreate.contactsReference.EntityKey =
                    new EntityKey("InternshipEntities.ContactSet", "ID", contactToCreate.ID);
                internshipToCreate.availabilityReference.EntityKey =
                    new EntityKey("InternshipEntities.AvailabilitySet", "ID", availabilityToCreate.ID);
                internshipToCreate.classificationsReference.EntityKey =
                    new EntityKey("InternshipEntities.ClassificationSet", "ID", internshipToCreate.classifications.ID);
                internshipToCreate.educationReference.EntityKey =
                    new EntityKey("InternshipEntities.EducationSet", "ID", internshipToCreate.education.ID);

                _internshipEntities.AddToInternshipSet(internshipToCreate); //exception here
                _internshipEntities.SaveChanges();

                return internshipToCreate;
            }
            catch(Exception e)
            {
                throw e; 
            }
        }
        catch(Exception e)
        {
            throw e;
        }
    }
    catch(Exception e)
    {
        throw e;
    }

}

当我跟踪时,除了错误之外没有给出其他信息,所以我什至不确定哪个键是问题所在。

编辑:这是最终工作的版本:

using (TransactionScope scope = new TransactionScope())
{
    try
    {
        Contact contactToCreate = new Contact();
        Availability availabilityToCreate = new Availability();
        Internship i = new Internship();

        // Set the contact entity values;

        contactToCreate.Fax = internshipToCreate.contacts.Fax;
        //...
        //ommited for brevity
        //...
        contactToCreate.ContactEmail = internshipToCreate.contacts.ContactEmail;

        // Set the contact entity references to existing tables

        contactToCreate.statesReference.EntityKey =
                    new EntityKey("InternshipEntities.StateSet", "ID", internshipToCreate.contacts.states.ID);
        contactToCreate.countriesReference.EntityKey =
                    new EntityKey("InternshipEntities.CountrySet", "ID", internshipToCreate.contacts.countries.ID);

        // Add contact

        _internshipEntities.AddToContactSet(contactToCreate);

        // Set the availability entity values;

        availabilityToCreate.StartDate = internshipToCreate.availability.StartDate;
        availabilityToCreate.EndDate = internshipToCreate.availability.EndDate;
        availabilityToCreate.Negotiable = internshipToCreate.availability.Negotiable;

        // Add availability

        _internshipEntities.AddToAvailabilitySet(availabilityToCreate);

        //Add contact and availability entities to new internship entity

        i.contacts = contactToCreate;
        i.availability = availabilityToCreate;

        // Set internship entity values;

        i.UserID = internshipToCreate.UserID;
        //...
        //ommited for brevity
        //...
        i.Created = DateTime.Now;

        // Set the internship entity references to existing tables

        i.classificationsReference.EntityKey =
            new EntityKey("InternshipEntities.ClassificationSet", "ID", internshipToCreate.classifications.ID);
        i.educationReference.EntityKey =
            new EntityKey("InternshipEntities.EducationSet", "ID", internshipToCreate.education.ID);

        // Add internship and save

        _internshipEntities.AddToInternshipSet(i);
        _internshipEntities.SaveChanges();

        //commit transaction
        scope.Complete();

        return internshipToCreate;

    }
    catch (Exception e)
    {
        throw e;
    }
}

I am attempting to add some entities that I have created. When I try and add the entity in question to the set (see code below) I get the following error:

"The object could not be added or attached because its EntityReference has an EntityKey property value that does not match the EntityKey for this object."

I can't tell what entitykey it's referring to though. Here is the code, there is probably a much better way to pull this off as well:

public Internship CreateInternship(Internship internshipToCreate)
{
    try
    {
        Contact contactToCreate = new Contact();

        contactToCreate.Fax = internshipToCreate.contacts.Fax;
        contactToCreate.Extension = internshipToCreate.contacts.Extension;
        contactToCreate.FirstName = internshipToCreate.contacts.FirstName;
        contactToCreate.MiddleName = internshipToCreate.contacts.MiddleName;
        contactToCreate.LastName = internshipToCreate.contacts.LastName;
        contactToCreate.PhoneNumber = internshipToCreate.contacts.PhoneNumber;
        contactToCreate.StreetAddress = internshipToCreate.contacts.StreetAddress;
        contactToCreate.PostalCode = internshipToCreate.contacts.PostalCode;
        contactToCreate.ContactEmail = internshipToCreate.contacts.ContactEmail;

        contactToCreate.statesReference.EntityKey =
                    new EntityKey("InternshipEntities.StateSet", "ID", internshipToCreate.contacts.states.ID);
        contactToCreate.countriesReference.EntityKey =
                    new EntityKey("InternshipEntities.CountrySet", "ID", internshipToCreate.contacts.countries.ID);

        _internshipEntities.AddToContactSet(contactToCreate);
        _internshipEntities.SaveChanges();

        try
        {
            Availability availabilityToCreate = new Availability();

            availabilityToCreate.StartDate = internshipToCreate.availability.StartDate;
            availabilityToCreate.EndDate = internshipToCreate.availability.EndDate;
            availabilityToCreate.Negotiable = internshipToCreate.availability.Negotiable;

            _internshipEntities.AddToAvailabilitySet(availabilityToCreate);
            _internshipEntities.SaveChanges();

            try
            {
                internshipToCreate.contactsReference.EntityKey =
                    new EntityKey("InternshipEntities.ContactSet", "ID", contactToCreate.ID);
                internshipToCreate.availabilityReference.EntityKey =
                    new EntityKey("InternshipEntities.AvailabilitySet", "ID", availabilityToCreate.ID);
                internshipToCreate.classificationsReference.EntityKey =
                    new EntityKey("InternshipEntities.ClassificationSet", "ID", internshipToCreate.classifications.ID);
                internshipToCreate.educationReference.EntityKey =
                    new EntityKey("InternshipEntities.EducationSet", "ID", internshipToCreate.education.ID);

                _internshipEntities.AddToInternshipSet(internshipToCreate); //exception here
                _internshipEntities.SaveChanges();

                return internshipToCreate;
            }
            catch(Exception e)
            {
                throw e; 
            }
        }
        catch(Exception e)
        {
            throw e;
        }
    }
    catch(Exception e)
    {
        throw e;
    }

}

There is no other information given besides the error when I trace through so I'm not even sure which Key is the issue.

EDIT: Here is the version that ended up working:

using (TransactionScope scope = new TransactionScope())
{
    try
    {
        Contact contactToCreate = new Contact();
        Availability availabilityToCreate = new Availability();
        Internship i = new Internship();

        // Set the contact entity values;

        contactToCreate.Fax = internshipToCreate.contacts.Fax;
        //...
        //ommited for brevity
        //...
        contactToCreate.ContactEmail = internshipToCreate.contacts.ContactEmail;

        // Set the contact entity references to existing tables

        contactToCreate.statesReference.EntityKey =
                    new EntityKey("InternshipEntities.StateSet", "ID", internshipToCreate.contacts.states.ID);
        contactToCreate.countriesReference.EntityKey =
                    new EntityKey("InternshipEntities.CountrySet", "ID", internshipToCreate.contacts.countries.ID);

        // Add contact

        _internshipEntities.AddToContactSet(contactToCreate);

        // Set the availability entity values;

        availabilityToCreate.StartDate = internshipToCreate.availability.StartDate;
        availabilityToCreate.EndDate = internshipToCreate.availability.EndDate;
        availabilityToCreate.Negotiable = internshipToCreate.availability.Negotiable;

        // Add availability

        _internshipEntities.AddToAvailabilitySet(availabilityToCreate);

        //Add contact and availability entities to new internship entity

        i.contacts = contactToCreate;
        i.availability = availabilityToCreate;

        // Set internship entity values;

        i.UserID = internshipToCreate.UserID;
        //...
        //ommited for brevity
        //...
        i.Created = DateTime.Now;

        // Set the internship entity references to existing tables

        i.classificationsReference.EntityKey =
            new EntityKey("InternshipEntities.ClassificationSet", "ID", internshipToCreate.classifications.ID);
        i.educationReference.EntityKey =
            new EntityKey("InternshipEntities.EducationSet", "ID", internshipToCreate.education.ID);

        // Add internship and save

        _internshipEntities.AddToInternshipSet(i);
        _internshipEntities.SaveChanges();

        //commit transaction
        scope.Complete();

        return internshipToCreate;

    }
    catch (Exception e)
    {
        throw e;
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

无可置疑 2024-08-11 11:19:03

你好,

虽然我不确定问题是什么,但我有一个建议。您传递到方法 (internshipToCreate) 的 Internship 对象用于将值传输到您在方法内部实例化的其他实体(联系人、可用性)以及它们的创建工作得很好。

也许你应该尝试在实习中做同样的事情?创建新的 Internship 对象并通过从实习ToCreate 对象获取它们来设置所有值,然后将新创建的对象传递给 _internshipEntities.AddToInternshipSet 方法。

您可能在实习ToCreate 对象上设置了一些用于其他目的所需的值,而其中一些值实际上导致了异常。

而且,我不知道您的业务逻辑是什么,但如果您将所有内容都放在一个事务下会更好,因为这样可能会创建前两个实体,而不会创建第三个实体。

Hallo,

although I'm not sure what the problem is I have a suggestion. The Internship object that you are passing into method (internshipToCreate) is used to transfer values to other entities (Contact, Availability) that you instantiated inside of the method, and their creation works just fine.

Maybe you should try to do the same with Internship? Create new Internship object and set all values you have by taking them from internshipToCreate object, and than that newly created object pass to the _internshipEntities.AddToInternshipSet method.

It is possible that you've set some values on internshipToCreate object that you needed for other purposes, and that some of those is actually causing the exception.

And, I don't know what you business logic is, but it would be better if you put all under one transaction, because like this it may happen that first two entities are created, and third one not.

恍梦境° 2024-08-11 11:19:03

这段代码对我来说没有多大意义。在两种情况下,当您可以分配对象引用时,您将使用 EntityKey。即,将其更改为:

internshipToCreate.contactsReference.EntityKey =
    new EntityKey("InternshipEntities.ContactSet", "ID", contactToCreate.ID);
internshipToCreate.availabilityReference.EntityKey =
    new EntityKey("InternshipEntities.AvailabilitySet", "ID", availabilityToCreate.ID);

...

internshipToCreate.contacts = contactToCreate;
internshipToCreate.availability = availabilityToCreate;

在其他两种情况下,您似乎试图分配已经存在的对象的 ID。在我看来,这两行即使成功,也无济于事:

internshipToCreate.classificationsReference.EntityKey =
    new EntityKey("InternshipEntities.ClassificationSet", "ID", internshipToCreate.classifications.ID);
internshipToCreate.educationReference.EntityKey =
    new EntityKey("InternshipEntities.EducationSet", "ID", internshipToCreate.education.ID);

所以你可以摆脱它们。

当您进行这两项更改时会发生什么?

This code isn't making a lot of sense to me. In two cases, you're going through an EntityKey when you could just assign an object reference. I.e, change this:

internshipToCreate.contactsReference.EntityKey =
    new EntityKey("InternshipEntities.ContactSet", "ID", contactToCreate.ID);
internshipToCreate.availabilityReference.EntityKey =
    new EntityKey("InternshipEntities.AvailabilitySet", "ID", availabilityToCreate.ID);

...to:

internshipToCreate.contacts = contactToCreate;
internshipToCreate.availability = availabilityToCreate;

In the other two cases you seem to be attempting to assign the ID of the object which is already there. These two lines, even if successful, it seems to me, would do nothing:

internshipToCreate.classificationsReference.EntityKey =
    new EntityKey("InternshipEntities.ClassificationSet", "ID", internshipToCreate.classifications.ID);
internshipToCreate.educationReference.EntityKey =
    new EntityKey("InternshipEntities.EducationSet", "ID", internshipToCreate.education.ID);

So you can just get rid of them.

What happens when you make these two changes?

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