Fluent Nhibernate - 一对一映射 - 更新/删除
我有两个类 Office 和 FileDataObject 定义为:
public class Office
{
public virtual string Name { get; set; }
public virtual string Phone { get; set; }
public virtual **FileDataObject** OfficePdf { get; set; }
}
public class FileDataObject
{
public virtual int? Id { get; set; }
public virtual ContentType ContentType {get;set;}
public virtual string FilePath {get;set;}
}
映射定义为:
public class OfficeMap : ClassMap<Office>
{
Map(x => x.Name).Not.Nullable();
Map(x => x.Phone).Length(20);
References<FileDataObject>(x => x.OfficePdf).Cascade.All().Column("OfficePdfId").Nullable().ForeignKey("FK_Office_FileDataObject");
}
public class FileDataObjectMap : ClassMap<FileDataObject>
{
Id(x => x.Id).UnsavedValue(null).GeneratedBy.Native();
Map(x => x.ContentType);
Map(x => x.FilePath);
}
对于单个办公室,我只需要一个 OfficePdf (FileDataObject),但当没有为特定办公室上传 pdf 时,它可能为 NULL。 (所以 0..1 关系)
上面两个的模式正在使用
正确生成 表:Office-列:OfficePdfId -> 映射 ->表的 PK-Id:FileDataObject。
请注意,我不希望 Table: FileDataObject 具有 OfficeId 列,因为 FileDataObject 也被其他一些实体使用。
虽然 FileDataObject 已正确保存,但我面临的问题是 -
当用户更新现有的 Office pdf 时,我很困惑是否要
首先删除现有的 FileDataObject,然后创建一个新的或
更新现有的pdf将是一个更好的选择。
或者FNH有什么办法可以解决这个问题吗?
能否请您指导一下。
谢谢你!
I have two classes Office and FileDataObject defined as:
public class Office
{
public virtual string Name { get; set; }
public virtual string Phone { get; set; }
public virtual **FileDataObject** OfficePdf { get; set; }
}
public class FileDataObject
{
public virtual int? Id { get; set; }
public virtual ContentType ContentType {get;set;}
public virtual string FilePath {get;set;}
}
with mappings defined as:
public class OfficeMap : ClassMap<Office>
{
Map(x => x.Name).Not.Nullable();
Map(x => x.Phone).Length(20);
References<FileDataObject>(x => x.OfficePdf).Cascade.All().Column("OfficePdfId").Nullable().ForeignKey("FK_Office_FileDataObject");
}
public class FileDataObjectMap : ClassMap<FileDataObject>
{
Id(x => x.Id).UnsavedValue(null).GeneratedBy.Native();
Map(x => x.ContentType);
Map(x => x.FilePath);
}
For a single office, I need just one OfficePdf (FileDataObject), but it MAY BE NULL when no pdf is uploaded for a particular office. (so a 0..1 relationship)
Schema for above two is being generated correctly with
Table:Office-Column:OfficePdfId ->mapping-> PK-Id of Table: FileDataObject.
Please note that I do not want Table: FileDataObject to have OfficeId column as FileDataObject is being used by some other entities as well.
Though FileDataObject is being saved correctly, the issue I am facing is -
When user updates an existing office pdf, I am confused whether to
first delete the existing FileDataObject and then create a new one or
Update the existing pdf will be a better choice.
Or is there any way FNH can take care of it?
Could you please guide.
Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
除非您有一些通过删除和重新创建来满足的特定要求,否则更新现有的可能是最简单的。您可以决定在
Office
类中创建或更新,因为它是FileDataObject
的所有者Unless you have some specific requirement that will be met by deleting and re-creating, it's probably easiest to just update the existing one. You can make the decision to create or update in your
Office
class, since it's the owner of theFileDataObject