我正在学习Entity Framework,所以如果这对您来说容易就不要怪我
我有班级HomeNumber.cs
[ScaffoldColumn(false)]
[Key]
public int HomeNumberId { get; set; }
//[ForeignKey("HomeNumberId")]
//public virtual HomeNumber HomeNumbers { get; set; }
[Required(ErrorMessage = "Polje kućni broj je obavezno!")]
public string HomeNumberName { get; set; }
[Required(ErrorMessage = "Polje pod broj/slovo je obavezno!")]
public string HomeSubNumber { get; set; }
public int StreetId { get; set; }
[ForeignKey("StreetId")]
public virtual Street Street { get; set; }
....
public bool AddHomeNumber(string homeNumberName, string homeSubNumber, Street street)
{
HomeNumber number = new HomeNumber();
number.HomeNumberName = homeNumberName;
number.HomeSubNumber = homeSubNumber;
number.StreetId = street.StreetId;
using (var _db = new DataContext())
{
// Dodaj HomeNumber u bazu [AD_HOMENUMBER]
_db.DB_HomeNumber.Add(number);
_db.SaveChanges();
}
// Success.
return true;
}
和Street.cs类
[ScaffoldColumn(false)]
[Key]
public int StreetId { get; set; }
[Required(ErrorMessage = "Polje naziv ulice je obavezno!")]
public string StreetName { get; set; }
.....
public bool AddStreet(string streetName)
{
Street street = new Street();
street.StreetName = streetName;
using (var _db = new DataContext())
{
// Dodaj Street u bazu [AD_STREET]
_db.DB_Street.Add(street);
_db.SaveChanges();
}
// Success.
return true;
}
我的背后代码是:
Street st = new Street();
st.AddStreet(ucAddress.Street.Text);
HomeNumber hou = new HomeNumber();
hou.AddHomeNumber(ucAddress.HouseNumber.Text, ucAddress.HouseSubNumber.Text, st.StreetId);
我需要在我的背后代码st.streetId中添加值,...我不知道我缺少什么,...因为在AddHomeNumber中,我调用street.StreetId,其中st.streetId为返回值
最佳答案
我认为您要在AddStreet
上执行的操作如下所示。您保存Street
并返回结果。
public Street AddStreet(string streetName)
{
Street street = new Street();
street.StreetName = streetName;
using (var _db = new DataContext())
{
// Dodaj Street u bazu [AD_STREET]
_db.DB_Street.Add(street);
_db.SaveChanges();
}
// Success.
return street;
}
在后面的代码中,您在
st
中传递了st.StreetId
而不是AddHomeNumber
。Street st = new Street();
st = st.AddStreet(ucAddress.Street.Text); // get the street that has been saved with generated id.
HomeNumber hou = new HomeNumber();
hou.AddHomeNumber(ucAddress.HouseNumber.Text, ucAddress.HouseSubNumber.Text, st);
注意:我建议您看一下Repository模式,这将使您的生活更轻松,并且将编写更具可读性和更出色的代码。
Street
的基本存储库对于HomeNumber
或以相同方式创建存储库的其他实体也将类似。public class StreetRepository
{
private DataContext _db;
public StreetRepository(DataContext dataContext)
{
_db = dataContext;
}
public Street Add(string streetName)
{
Street street = new Street();
street.StreetName = streetName;
using (var _db = new DataContext())
{
// Dodaj Street u bazu [AD_STREET]
_db.DB_Street.Add(street);
_db.SaveChanges();
}
// Success.
return street;
}
public Street Get(int id)
{
return _db.DB_Street.Find(id);
}
// other CRUD methods
}
用法:
StreetRepository streetRepository = new StreetRepository();
HomeNumberRepository homeNumberRepository = new HomeNumberRepository(); // you have to create it yourself
Street st = streetRepository.Add(ucAddress.Street.Text); // get the street that has been saved with generated id.
HomeNumber homeNumber = homeNumberRepository.Add(ucAddress.HouseNumber.Text, ucAddress.HouseSubNumber.Text, st);
注意:您可能会发现为每个实体编写一个存储库都很痛苦,这里有帮助通用存储库。
掌握了Repository模式之后,您可以转到工作单元并发现EntityFramework已经有了它。