public class Folder
{
[Key]
public string Name { get; set; }
public virtual ICollection<File> Files { get; set; }
}
public class File
{
[Key, Column(Order = 0)]
public string FolderName { get; set; }
[Key, Column(Order = 1)]
public DateTime FileName { get; set; }
public virtual Folder Folder { get; set; }
}
Chanllange 来装饰File类的方法,以便:
new File() {Folder = folder, FileName= "bla-bla"};
按folder.Name
的值自动设置File对象的FolderName属性是否可以覆盖属性?
能力水平2
public class Folder
{
[Key, Column(Order = 0)]
public string PC { get; set; }
[Key, Column(Order = 1)]
public string Name { get; set; }
public virtual ICollection<File> Files { get; set; }
}
public class File
{
[Key, Column(Order = 0)]
public string PC { get; set; }
[Key, Column(Order = 1)]
public string FolderName { get; set; }
[Key, Column(Order = 2)]
public DateTime FileName { get; set; }
public virtual Folder Folder { get; set; }
}
同样的问题 如何在指令
之后训练模型自动设置外键new File() {Folder=folder, FileName='bla-bla'}
更新 非常简短,如何在属性
上写下来HasRequired(a => a.Folder).WithMany(b => b.Files).HasForeignKey(c => new { c.Pc, c.FolderName});
答案 0 :(得分:1)
只需将Folder
属性从auto-implemented更改为包含支持字段的属性,然后在其setter中设置FolderName
。还要将外键属性添加到此属性:
public class File
{
private Folder _folder;
[Key, Column(Order = 0)]
public string FolderName { get; set; }
[Key, Column(Order = 1)]
public DateTime FileName { get; set; }
[ForeignKey("FolderName")]
public virtual Folder Folder
{
get { return _folder; }
set
{
if (value == null)
throw new ArgumentNullException();
_folder = value;
FolderName = _folder.Name;
}
}
}