私はWebMatrixとEntityFrameworkを使用しています。
私のデータベースにはユーザーテーブルがあり、ビジネスロジックでは、ユーザーは別のテーブルのコンテンツを作成または更新できるため、モデルのユーザークラスは次のようになります。
[Table("Users")]
public class User
{
[Key]
public int UserId { get; set; }
[MaxLength(50), MinLength(5), Required]
public string UserName { get; set; }
[MaxLength(16), MinLength(8), Required]
public string Password { get; set; }
[MaxLength(50), Required]
public string Name{ get; set; }
[MaxLength(50), Required]
public string LasName1{ get; set; }
[MaxLength(50), Required]
public string LasName2{ get; set; }
public string token { get; set; }
public DateTime CreatedDateTime { get; set; }
public DateTime UpdatedDateTime { get; set; }
[InverseProperty("CreatedBy")]
public virtual ICollection<Device> CreatedDevices {get; set;}
[InverseProperty("UpdatedBy")]
public virtual ICollection<Device> UpdatedDevices {get; set;}
[InverseProperty("CreatedBy")]
public virtual ICollection<Cluster> CreatedClusters {get; set;}
[InverseProperty("UpdatedBy")]
public virtual ICollection<Cluster> UpdatedClusters {get; set;}
[InverseProperty("CreatedBy")]
public virtual ICollection<Region> CreatedRegions {get; set;}
[InverseProperty("UpdatedBy")]
public virtual ICollection<Region> UpdatedRegions {get; set;}
}
たとえば、私の地域クラスは次のようになります。
[Table("Regions")]
public class Region
{
[Key]
public int RegionId { get; set; }
[MaxLength(50), MinLength(5), Required]
public string Name{ get; set; }
public virtual ICollection<Cluster> Clusters { get; set; }
public DateTime CreatedDateTime { get; set; }
public DateTime UpdatedDateTime { get; set; }
public Usuario CreatedBy { get; set; }
public Usuario UpdatedBy { get; set; }
}
これにより、データベース内のエンティティ間に必要な関係が作成されます。ここで私の主な疑問があります。クラスターテーブルにはCreatedBy_UserIdがあるので、テーブルのエントリの挿入または更新を行うときに、Userオブジェクトを渡す必要がありますか?私はこのようなことを言っています
Cluster cluster = new Cluster()
{
RegionId = regionId,
CreatedDateTime = createdDateTime,
UpdatedDateTime = updatedDateTime,
CreatedBy = currentUser,
UpdatedBy = currentUser
};
または、外部キーを表すintをテーブルに渡すにはどうすればよいですか?