Using Additional Data on Intermediate Table with N

2019-01-28 23:55发布

问题:

I'm working on a project that is used to input information about a product into a document. One section has a form that can be filled out as many times as needed for a particular product. The information on this form is then available to be shared on multiple related documents. A typical many-to-many situation. The difference is, on the intermediate table, I want to store specific information for that form entry for that specific document.

Here are some table definitions to illustrate the point:

Document(
  Id int identity(1,1),
  Name nvarchar(256)
)

Form(
  Id int identity(1,1),
  FormData nvarchar(max)
)

DocumentForms(
  DocumentId int,
  FormId int,
  DocumentFormNumber varchar(64)
)

And some classes:

public class Document{
  public int Id { get; protected set; }
  protected IList<Form> Forms { get; set; }
}

public class Form {
  public int Id { get; protected set; }
  public string DocumentFormNumber { get; set; }
  public string FormData { get; set; }
}

Is there a way to create a mapping that supports reading the additional data when reading forms as part of the many to many relationship with Document? Do I need to use a stored procedure when loading data with the many to many relationship? I still need to be able to load the Form objects without them being a part of a document, but once they are added to the document, I need them to have a number that is generated based on some simple rules. But that number is obviously document specific. So while the next document might use the same Form data, it needs a new number.

Any suggestions on how this can be achieved?

回答1:

I found my answer in a blog post by Billy McCafferty here.