English 中文(简体)
删除一对一的关系中的相关实体
原标题:deleting related entities in a one-to-many relationship

我有这个领域:

public class Phone {
    public int Id { get; set; }
    public string Number { get; set; }
    public Person Person { get; set; }
}
public class Person {
    public int Id { get; set; }
    public IList<Phone> Phones { get; set; }
}

I load a Person and clear its Phones. But the operation cause an error:

// actually loads person from repository...
var person = _personRepository.Include(p => p.Phones).Where(p => p.Id == 1).First();
person.Phones.Clear();
_personRepository.Update(person);

您可以看到装上<代码>Person的简单逻辑,并填写<代码>Phones。 但出现这一错误:

The operation failed: The relationship could not be changed because one or more of the foreign-key properties is non-nullable. When a change is made to a relationship, the related foreign-key property is set to a null value. If the foreign-key does not support null values, a new relationship must be defined, the foreign-key property must be assigned another non-null value, or the unrelated object must be deleted.

实际上,我想澄清所有<代码>Person.Phones,并增加一些新项目。 但我想在一个问题中予以澄清,不要逐一删除。

你们是否有任何想法? 您能否帮助我? 提前感谢。

最佳回答

你们能够产生欧洲能源论坛的基础。 因此,在电子格式中,没有办法生成一份单项内容说明,删除所有<>Phone记录,并附上<代码>Person。 Id 。

页: 1 ObjectContext.Executesqlcommand%28v=VS.103%29.aspx”rel=“nofollow” 取决于您的模式。

问题回答
foreach(var phone in person.Phones)
{
   context.DeleteObject(phone);
}
person.Phones.Clear();

可能提供帮助。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签