所以我在Model文件夹中创建了person,address,personaddress类 人
public class Person
{
public int Id {}
public string Name {}
}
地址
public class Address
{
public int Id {}
public string Address{}
}
personaddress
public class PersonAddress
{
public int PersonId {}
public string AddressId {}
}
我还在Model文件夹中创建了iperson,iaddress,ipersonaddress类, 界面IPerson
{
IEnumerable<Person> GetAll();
Person Get(int id);
Person Add(Person pers);
void Remove(int id);
bool Update(Person pers);
}
interface IAddress
{
IEnumerable<Address> GetAll();
AddressGet(int id);
AddressAdd(Address addr);
void Remove(int id);
bool Update(Address addr);
}
interface IPersonAddress
{
IEnumerable<PersonAddress> GetAll();
PersonAddressGet(int id);
PersonAddressAdd(PersonAddress persaddr);
void Remove(int id);
bool Update(PersonAddress persaddr);
}
然后我在模型文件夹
中创建了personrepository,addressrepository,personaddressrepositorypublic class PersonRepository : IPersonRepository
{
private List<Person> people= new List<Person>();
private int _nextId = 1;
public PersonRepository()
{
Add(new Person{ Name = "Peter Smith" });
Add(new Person{ Name = "Joe Doe" });
}
public IEnumerable<Person> GetAll()
{
return people;
}
public PersonGet(int id)
{
return people.Find(p => p.Id == id);
}
public PersonAdd(Person pers)
{
if (pers== null)
{
throw new ArgumentNullException("pers");
}
pers.Id = _nextId++;
people.Add(pers);
return pers;
}
public void Remove(int id)
{
people.RemoveAll(m => m.Id == id);
}
public bool Update(Person pers)
{
if (pers==null)
{
throw new ArgumentNullException("pers");
}
int index = people.FindIndex(p => p.Id == pers.Id);
if (index ==-1)
{
return false;
}
people.RemoveAt(index);
people.Add(pers);
return true;
}
}
然后我为这3个人,地址,人物地址添加了控制器
public class PersonController : ApiController
{
static readonly IPersonRepository repository = new PersonRepository();
public IEnumerable<Person> GetPersonByName(string name)
{
return repository.GetAll().Where(p => p.Name.ToLower().Contains(name.ToLower()));
}
public HttpResponseMessage AddPerson(Person pers)
{
pers= repository.Add(pers);
var response = Request.CreateResponse<Person>(HttpStatusCode.Created, pers);
string uri = Url.Link("DefaultApi", new { id = pers.Id });
response.Headers.Location = new Uri(uri);
return response;
}
public void UpdatePerson(int id, Person person)
{
person.Id = id;
if (!repository.Update(person))
{
throw new HttpResponseException(HttpStatusCode.NotFound);
}
}
public void DeletePerson(int id)
{
Person pers= repository.Get(id);
if (pers== null)
{
throw new HttpResponseException(HttpStatusCode.NotFound);
}
repository.Remove(id);
}
}
答案 0 :(得分:0)
你有几种可能性。这是其中两个。
将public virtual ICollection<Address> Addresses{ get; set; }
添加到您的模型类中,并在Constructor
Addresses = new Collection<Addresses>();
内进行初始化。这将启用延迟加载相关子集合。对Address
类做同样的事情。
急切地使用Include()
和Get()
方法中的GetAll()
加载它们。
示例:context.Persons.Include("Address").Where(x => x.PersonId == Id).ToList();
或使用Linq
删除:
var person = context.Persons.GetById(personId);
foreach(var personAddress in person.Addresses)
{
person.Addresses.Remove(personAddress);
context.SaveChanges();
}
或者
var person = context.Persons.GetById(personId);
person.Addresses.Load();
person.Addresses.ToList().ForEach(x => context.Addresses.DeleteObject(x));
编辑:样本人员类
public class Person()
{
public Person()
{
Addresses = new Collection<Address>();
}
public int Id {get; set;}
public string Name {get; set;}
public virtual Collection<Address> Addresses {get; set;}
}