0
단순한 관계 모델이라고 생각하는 것을 이해하는 데 어려움을 겪고 있습니다. CDN의 파일을 나타내는 CloudFile
개체가 있습니다. 나는 CloudFile
과 관련된 두 개의 다른 객체 인 Creative
과 Merchant
을 가지고 있습니다.엔터티 프레임 워크와 일대 다 관계 첫 번째 코드
내 CloudFile 클래스
public class CloudFile
{
[Key]
public int CloudFileId { get; set; }
[StringLength(50)]
public string OriginalFileName { get; set; }
[StringLength(100)]
public string Name { get; set; }
[StringLength(5)]
public string FileExt { get; set; }
public int Height { get; set; }
public int Width { get; set; }
public long SizeInBytes { get; set; }
public DateTime DateCreated { get; set; }
public CloudFile()
{
this.DateCreated = DateTime.Now;
}
}
내 크리에이티브 클래스
public class Creative
{
[Key]
public int CreativeId { get; set; }
[Required]
public int OfferId { get; set; }
public virtual Offer Offer { get; set; }
public virtual CloudFile CloudFile { get; set; }
[StringLength(100)]
public string Alt { get; set; }
public DateTime DateCreated { get; set; }
public Creative()
{
this.DateCreated = DateTime.Now;
this.CloudFile = new CloudFile();
}
}
내 상인 클래스
public class Merchant
{
#region Keys
[Key]
[Display(Name = "Merchant ID")]
public int MerchantId { get; set; }
#endregion
[Required]
[Display(Name = "Merchant Name")]
[StringLength(256)]
public string Name { get; set; }
public virtual CloudFile CloudFile { get; set; }
public ICollection<Offer> Offers { get; set; }
public ICollection<MerchantLocation> Locations { get; set; }
public Merchant()
{
this.Offers = new List<Offer>();
this.Locations = new List<MerchantLocation>();
}
데이터베이스에 이러한 개체를 올바르게 연결하고 저장하는 방법을 모르겠습니다. 나는 Merchant.CloudFile에 새로운 CloudFile 객체를 추가하고 다음 실행 시도 :
......
merchant.CloudFile = new CloudFile() {...set properties here...};
MerchantDB.Modify(merchant);
......
public static bool Modify(Merchant merchant)
{
using (AppDbContext db = new AppDbContext())
{
db.Entry(merchant).State = EntityState.Modified;
//Return true if only 1 record was modified. If not, return false.
return (db.SaveChanges() == 1);
}
}
을하지만 그것은 CloudFile을 저장하고 상인과 연관되지 않습니다. CloudFile 테이블에 CloudFile을 직접 저장할 수 있지만 아직 연관이 없습니다.
미리 도움을 주셔서 감사합니다.
당신은 두 가지 방법으로 관계를 언급하지 않았 때문이다
에 그 CloudFile에 대한 참조가 필요합니다. CloudFile 클래스에도 추가해야합니다. 저 거꾸로? – FrankO
FrankO : 내가 따르는 엄지 손가락 규칙은 항상 양방향 관계를 언급하는 것입니다. 그리고 cloudfile에서 상인으로 이동하려면이 것이 있어야합니다. 사실 관계가 1 대 1이면 db에 있어야합니다. 일부 테이블 이름을 만들었습니다. CloudFileMerchant –
의견을 보내 주셔서 감사합니다. 난 그냥 모든 판매자, 크리 에이 티브 또는 클라우드 CDN에있는 다른 비즈니스 엔티티 파일을 보유 CloudFiles라는 단일 테이블을 만들려고했다. 이것이 올바른 접근법이 아닌가? – FrankO