简体   繁体   English

EF InvalidCastException具有一对多关系

[英]EF InvalidCastException With one to many relationship

I have am getting an InvalidCaseException when adding a one to many relationship entity. 将一对多关系实体添加到一个实体时,出现InvalidCaseException异常。 It fails on the Add not on the SaveChanges. 它在添加而不是在SaveChanges上失败。

Unable to cast object of type 'System.Collections.Generic.List`1[UserAccount]' to type 'UserAccount'. 无法将类型为“ System.Collections.Generic.List`1 [UserAccount]”的对象转换为类型为“ UserAccount”的对象。

It is tring to case a collection of X to one instance of X 试图将X的集合放在X的一个实例上

(Parent or One Entity) (父母或一个实体)

[System.Xml.Serialization.XmlRootAttribute(Namespace="", IsNullable=false), Serializable()]
public class Merchant
{   

    /// <summary>
    /// Standard ID
    /// </summary>
    [Key]
    [DatabaseGenerated(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.Identity)]
    public int ID { get; set; }
    [DataType(DataType.Text), MaxLength(100)]
    /// <summary>
    /// UserFriendly Name 
    /// </summary>
    public string MerchantName { get; set; }

    /// Billing Information
    /// </summary>
    public virtual ICollection<BillingTransactions> BillingTransactions { get; set; }

    /// <summary>
    /// List of Accounts for this merchant  (pulled from DBAccounts)
    /// </summary>
    public virtual ICollection<UserAccount> UserAccounts { get; set; }
 }

(The Child or Many) (孩子还是很多)

public class UserAccount
{

    private string loginID;
    [Key]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    /// <summary>
    /// Standard ID 
    /// </summary>
    public int ID { get; set; }

    public int MerchantId { get; set; }

    [ForeignKey("ID")]    
    public Merchant Merchant { get; set; }
  //Obviously there are more properties here....
 }

I create the new entity as follows: 我创建新实体,如下所示:

 public void CreateNewMerchant(UserAccount useraccount)
    {
        Merchant merchant;
        if (useraccount.Merchant == null) //New unknown merchant
        {
            merchant = new Model.Merchant.Merchant();
            merchant.UserAccounts = new List<UserAccount>();
            merchant.UserAccounts.Add(useraccount);
        }
        else
        {
            merchant = useraccount.Merchant;
        }

        ServiceBase<Merchant> sb = new Core.ServiceBase<Merchant>();
        base.Add(merchant); 
        base.Save();




    }

This is for a wizard like form interface. 这是用于表单界面之类的向导的。 The first step is to create a useraccount. 第一步是创建一个用户帐户。 The next step is to fill in the new merchant information. 下一步是填写新的商家信息。 The Wizard steps are creating the child object useraccount and an empty parent and then creating the parent in the next step. 向导步骤将创建子对象useraccount和一个空的父对象,然后在下一步中创建父对象。

My code is creating a blank/empty parent and adding the child/useraccount to the empty merchant and adding to the database. 我的代码是创建一个空白/空的父级,并将子级/用户帐户添加到空商家,然后添加到数据库。

Why am I getting an invalid cast exception? 为什么会收到无效的强制转换异常?

You need... 你需要...

[ForeignKey("MerchantId")]
public Merchant Merchant { get; set; }

...instead of [ForeignKey("ID")] which will use the primary key as the foreign key and hence define a one-to-one instead of a one-to-many relationship. ...而不是[ForeignKey("ID")] ,它将使用主键作为外键并因此定义一对一而不是一对多的关系。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM