inutan
inutan

Reputation: 10888

Fluent Nhibernate - One to One Mapping - Update/Delete

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!

Upvotes: 2

Views: 342

Answers (1)

Kendrick
Kendrick

Reputation: 3787

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

Upvotes: 1

Related Questions