Fluent Nhibernate - 一对一映射 - 更新/删除

发布于 2024-11-09 16:23:20 字数 1384 浏览 2 评论 0原文

我有两个类 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 -> 映射 ->表的 P​​K-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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一个人练习一个人 2024-11-16 16:23:20

除非您有一些通过删除和重新创建来满足的特定要求,否则更新现有的可能是最简单的。您可以决定在 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 the FileDataObject

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文