我搜索並找到以下鏈接link a和link b,這些鏈接沒有解釋我做錯了什麼。控制器將參考實例保存爲新實例,而不是更新現有參考實例
我使用MVC3 +流利+ NHibernate的用流利的automapper模式具有以下配置:
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>
控制器保存,但不是在保存過程新的語言被創建,而不是更新的簡單分配領域的語言,現有的。檢查傳遞給控制器的Account對象,我們注意到它有一個對已經存在的id的Language對象的引用。這是爲任何參考映射完成的。
[HttpPost]
[NeedsPersistence]
public ActionResult Create(Account account)
{
if (ModelState.IsValid)
{
accountDao.Save(account, currentUser);
return RedirectToAction("Tree");
}
else {
return View();
}
}
如何獲得保存以識別語言已存在?
剛纔忘了粘貼XX的代碼。 Utils.SelectList – user616397 2011-03-01 08:35:47