EntityDataModel在插入或更新时将父键设置为null

时间:2011-08-03 18:49:42

标签: asp.net ado.net-entity-data-model

我在asp.net 3.5 sp1中工作。我有两张桌子

User
  UserID PK (int)
  UserName (varchar)
  ContactInfoID FK (int) Allow Null

ContactInfo
  ConactInfoID PK (int)
  Address (varchar)
  City (varchar)
  State (varchar)
  Zip (varchar)

我正在尝试使用EDM插入/更新没有相应父级ContactInfo的用户。数据库允许这样做。

我希望能够设置ContactInfo_ContactInfoID = -1来表示空父级。 我在User部分类中有以下功能。当我尝试将EntityKey的不同方面设置为null时,我收到了错误。

        public int ContactInfo_ContactInfoID
        {
            get
            {
                if (this.ContactInfoReference == null)
                    return -1;

                if (this.ContactInfoReference.EntityKey == null)
                    return -1;

                return (int)this.ContactInfoReference.EntityKey.EntityKeyValues [0].Value;
            }
            set
            {
                //To notify others listening that the property is changing
                this.OnPropertyChanging ("ContactInfoReference_ContactInfoD");

                if (value != -1)
                {
                    //Always needs to create a new EntityKey object
                    this.ContactInfoReference.EntityKey =
                        new EntityKey (
                            "ProjectRedEntities.ContactInfo",
                            new EntityKeyMember []
                                { new EntityKeyMember("ContactInfoID",
                                    (object)value) });
                }
                else
                {
//                    this.ContactInfoReference.EntityKey = null;
                    // ERROR: A relationship is being added or deleted from an AssociationSet 'FK_User_ContactInfo'. With cardinality constraints, a corresponding 'User' must also be added or deleted.

//                    this.ContactInfoReference.Value = null;
                    // ERROR : A relationship is being added or deleted from an AssociationSet 'FK_User_ContactInfo'. With cardinality constraints, a corresponding 'User' must also be added or deleted.
//                    this.ContactInfoReference.EntityKey =
//                        new EntityKey (
//                            "ProjectRedEntities.ContactInfo", "ContactInfoID", null);
                    // ERROR: Value cannot be null.

//                    this.ContactInfoReference.EntityKey = EntityKey.NoEntitySetKey;
                    // ERROR: The EntityKey property cannot be set to EntityNotValidKey, NoEntitySetKey, or a temporary key.

//                    this.ContactInfoReference.EntityKey =
//                        new EntityKey ();
                    // ERROR The EntityKey property cannot be set to EntityNotValidKey, NoEntitySetKey, or a temporary key.
                }
                //To notify others listenging that the property has changed
                this.OnPropertyChanged ("ContactInfoReference_ContactInfoID");
            }
        }

如果我尝试将关系中的可空字段设置为true,那么我会收到编译错误

对于ContactInfo类型,

'ContactInfoID'无效。密钥的所有部分都必须是不可为空的。

我可以从具有null ContactInfoID的数据库加载用户,但是当我尝试将EntityKey设置为null时,我会得到异常。

如何将User.ContactInfoID设置为null?

1 个答案:

答案 0 :(得分:0)

User.ContactID不能设置为-1来表示缺失值;而不是与实体键混淆,你不能使用null来做到这一点? EF不是为了使用-1而构建的......