Controller将引用的实例保存为新实例,而不是更新现有实例

时间:2011-02-14 16:06:23

标签: hibernate asp.net-mvc-3 fluent

我搜索并找到以下链接link alink b,这些链接无法解释我的错误。

我正在使用MVC3 + Fluent + NHibernate和Fluent自动化模式,具有以下配置:

NHibernate.Cfg.Configuration().Configure();
XXConfiguration autoMappingConf = new XXConfiguration();
FluentConfiguration fc = Fluently.Configure()
     .Database(MsSqlConfiguration.MsSql2008.ConnectionString(
         ConfigurationManager.ConnectionStrings[myConnString].ConnectionString
      )
      .ShowSql());
fc.ExposeConfiguration(cfg =>      cfg.SetProperty(NHibernate.Cfg.Environment.CurrentSessionContextClass, "web"));
fc.Mappings(m => m.AutoMappings.Add(AutoMap.AssemblyOf<BasicEntity>(autoMappingConf)
                .UseOverridesFromAssemblyOf<AccountMappingOverride>()
                .Conventions.Add(DefaultCascade.All()))
                );
        return fc.BuildSessionFactory();
    }

并使用以下覆盖:

public class AccountMappingOverride: IAutoMappingOverride<Account>
{
    public void Override(AutoMapping<Account> mapping)
    {
        mapping.HasMany(a => a.ChildrenAccounts).KeyColumn("ParentAccount_id");
    }
}

我有以下型号:

public abstract class Entity<TId>
{
    public virtual TId Id { get; set; }
    public virtual int ExternalId { get; set; }
}

public abstract class BasicEntity : Entity<int>{}

public abstract class MainEntity : BasicEntity
{
    public virtual int Version { get; set; }
    public virtual bool Active { get; set; }
}

public class Language : MainEntity 
{
    public virtual string Code { get; set; }
    public virtual string Name { get; set; }
}

public class Account : MainEntity
{
    public virtual string Name { get; set; }
    public virtual Language Language { get; set; }
    public virtual Account ParentAccount { get; set; }
    public virtual IList<Account> ChildrenAccounts { get; set; }
    public virtual IList<User> Users { get; set; }

}

 public class User : MainEntity
{
    public virtual string Name { get; set; }
    public virtual string Surname { get; set; }
    public virtual string Email { get; set; }
    [DataType(DataType.Password)]
    public virtual string Password { get; private set; }
    public virtual Language Language { get; set; }
    public virtual Account Account { get; set; }
    public virtual DateTime? LastLoginDate { get; set; }
    public virtual DateTime? LastPasswordChangeDate { get; set; }
}

显示用于创建新帐户的字段的视图:

@model XX.Models.Account

@{
   ViewBag.Title = "New Account";
   Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>New account</h2>

@using (Html.BeginForm())
{
    @Html.ValidationSummary(true)

    <div class="editor-label">
        @Html.LabelFor(model => model.Name)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.Name)
        @Html.ValidationMessageFor(model => model.Name)
    </div>


    <div class="editor-label">
        @Html.LabelFor(model => model.ParentAccount)
    </div>
    <div class="editor-field">
        @Html.DropDownListFor(model => model.ParentAccount.Id, XX.Utils.SelectList<XX.Models.Account>("Name", (string)ViewBag.parentAccountId))
        @Html.ValidationMessageFor(model => model.ParentAccount)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.Language)
    </div>
    <div class="editor-field">
        @Html.DropDownListFor(model => model.Language.Id,  XX.Utils.SelectList<XX.Models.Language>("Name", ""))
        @Html.ValidationMessageFor(model => model.Language)
    </div>

    <p>
        <input type="submit" value="Add" />
    </p>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

Controller保存,但不是简单地指定字段语言,而是在保存过程中创建新语言而不是更新现有语言。检查传递给控制器​​的Account对象,我们注意到它引用了一个已经存在id的Language对象。这适用于任何参考映射。

 [HttpPost]
 [NeedsPersistence]
 public ActionResult Create(Account account)
    {
        if (ModelState.IsValid)
        {
            accountDao.Save(account, currentUser);
            return RedirectToAction("Tree");
        }
        else {
            return View();
        }
    }

如何让保存识别语言已存在?

2 个答案:

答案 0 :(得分:0)

见这一行:

@ Html.DropDownListFor(model =&gt; model.Language.Id,XX.Utils.SelectList(“Name”,“”))

您告诉显示字段是什么,但数据字段是空字符串。

我想它会是这样的:

@ Html.DropDownListFor(model =&gt; model.Language.Id,XX.Utils.SelectList(“Name”,“Id”))

答案 1 :(得分:0)

我们通过定义ViewModels(我们将Language属性从模型中删除,但同样的情况适用于ParentAccount)和控制器,如下所示解决了这个问题:

public class AccountEdit
{
    public int Id { get; set; }
    [Required]
    [LocalizedDisplayName("Name")]
    public string Name { get; set; }
    [LocalizedDisplayName("ParentAccount")]
    public int ParentAccount { get; set; }
    [LocalizedDisplayName("Role")]
    public Role Role { get; set; }

    public AccountEdit() { }

    public AccountEdit(Account a)
    {
        Id = a.Id;
        Name = a.Name;
        ParentAccount = a.ParentAccount!=null ? a.ParentAccount.Id : 0;
        Role = a.Role;
    }

    public Account BoundAccount()
    {
        DataAccessObject<Account> dao = new DataAccessObject<Account>();
        Account account = dao.Get(Id);
        account.Name = Name;
        if (ParentAccount == default(int))
            account.ParentAccount = null;
        else
            account.ParentAccount = dao.Get(ParentAccount);
        return account;
    }
}

    [HttpPost]
    [NeedsPersistence]
    public ActionResult CreateDistributor(DistributorCreate dc)
    {
        if (ModelState.IsValid)
        {
            Account account = dc.BoundAccount();
            accountDao.Save(account, currentUser);
            return RedirectToAction("Index");
        }
        else {
            return View(dc);
        }
    }