我創建了一個通用的資源庫,它傳遞給它的類型,當我創建數據輸入方法時,實體創建的很好,但是當我創建一個鏈接的實體時,我又獲得了基礎實體。任何想法爲什麼?重複條目
詳細..
我已經劃分的規範到多個表來管理的東西...
現在我有一個人實體,申請實體...(在現實中申請人和人是同樣),一個承包商實體。承包商只能由申請人創建,因此始終創建申請人,因此總是會創建一個人。
當我開始創造一個人時,它會創造一個好人,但是當我創建一個申請人時,它會再次創造一個人。同樣,當我創建一個承包商時,由於某種原因它創建了一個人和多個申請人。
這是我的LINQ to SQL。如果你注意到我可以改進這個代碼,我也會理解的。
這裏是倉庫
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Linq;
namespace ParkingPermit.Models
{
public class UniversalManagerRepository<T> :IRepositoryT<T>
where T:class
{
private Table<T> _table;
private readonly DB _db ;//= new DB();
public UniversalManagerRepository()
{
_db = new DB();
_table = _db.GetTable<T>();
}
#region IRepositoryT<T> Members
public T Create(T create)
{
// _table = new DB().GetTable<T>();
//_db.GetTable(typeof(T)).InsertOnSubmit(create);
_table.InsertOnSubmit(create);
Save();
return create;
}
public void Delete(T delete)
{
throw new NotImplementedException();
}
public T Edit(T edit)
{
throw new NotImplementedException();
}
public T GetItem(int id)
{
throw new NotImplementedException();
}
public T Update(T update)
{
throw new NotImplementedException();
}
public IEnumerable<T> List()
{
//IQueryable i = _db.GetTable(typeof(T)).AsQueryable() ;
return _db.GetTable(typeof(T)) as IEnumerable<T>;
//throw new NotImplementedException();
}
public void Save()
{
//_db.SubmitChanges();
_table.Context.SubmitChanges();
//throw new NotImplementedException();
}
#endregion
}
}
我可以張貼LINQ to SQL設計的圖像是否有幫助,但我不能在這裏看到的功能...
非常感謝alt text http://img509.imageshack.us/img509/2072/linq.jpg
的事情是,當申請人被添加並申請人。人從會話中分配(在模型綁定器中),它創建一個新的人,這實際上是在開始時創建的原始人。我怎樣才能避免這一點。
protected override void OnModelUpdated(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
var personType = (Person)controllerContext.HttpContext.Session[PersonSessionKey];
controllerContext.HttpContext.Session[CurrentApplicantSessionKey] = null;
var av = new ApplicantValidator(new ModelStateWrapper(bindingContext.ModelState));
var newApplicant = bindingContext.Model as Applicant;
if (personType == null)
{
bindingContext.ModelState.AddModelError(bindingContext.ModelName,
"Cannot Update this Instance directly, please restart the application");
// controllerContext.HttpContext.Session[PersonSessionKey] = personType;
}
else if (newApplicant != null)
{
if (newApplicant.Person != null)
{
if (newApplicant.Person.Equals(personType as Person))
{
bindingContext.ModelState.AddModelError(bindingContext.ModelName,
"A person with these details already exists, please restart the application...");
//return
controllerContext.HttpContext.Session[PersonSessionKey] = null;
personType = null;
}
}
else if (av.Validate(newApplicant))
{
if (newApplicant.Person == null)
{
newApplicant.Person = personType as Person;
newApplicant.PersonId = personType.PersonId;
}
}
}
}
的事情是,當申請加入和applicant.Person從會話分配,它會創建一個新的人,這實際上是原來的人在開始時創建。我怎樣才能避免這一點。 – user182630 2009-11-02 14:37:51