Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The below creates a single table but allows for a reference/navigation property to a field you may not always wish to load (in this case, a byte array called <code>Data</code>). This is known as table splitting.</p> <p>Key notes are to have both entities map to the same table (duh) using <code>TableAnnotion</code> and share the same primary key, which is used as the foreign key for the nav properties to one another.</p> <pre><code> [Table("Document")] public class Document { [Key] [ForeignKey("Data")] [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public int DocumentId { get; set; } public string FileName { get; set; } public DateTime Created { get; set; } public virtual DocumentData Data { get; set; } } [Table("Document")] public class DocumentData { [Key] [ForeignKey("Document")] public int DocumentId { get; set; } public byte[] Data { get; set; } public virtual Document Document { get; set; } } public class DocEntities : DbContext { public DocEntities() : base("name=TestEntitiesCodeFirst") { } public DbSet&lt;Document&gt; Documents { get; set; } } static void Main(string[] args) { using (var db = new InheritTest.DocEntities()) { var doc = new Document() { Created = DateTime.Now, FileName = "Abc.txt", Data = new DocumentData() { Data = new byte[] { 0x50, 0x51, 0x52, 0x53 } } }; db.Documents.Add(doc); db.SaveChanges(); } using (var db = new InheritTest.DocEntities()) { db.Configuration.LazyLoadingEnabled = false; var doc = db.Documents.First(); if (doc.Data == null) { Console.WriteLine("doc.Data is null"); } db.Entry(doc).Reference(p =&gt; p.Data).Load(); if (doc.Data != null) { Console.WriteLine("doc.Data is not null"); Console.WriteLine(doc.Data.Data.Length); } } var input = Console.ReadLine(); } </code></pre> <p>Resulting table:</p> <pre><code>CREATE TABLE [dbo].[Document]( [DocumentId] [int] IDENTITY(1,1) NOT NULL, [FileName] [nvarchar](max) NULL, [Created] [datetime] NOT NULL, [Data] [varbinary](max) NULL, CONSTRAINT [PK_dbo.Document] PRIMARY KEY CLUSTERED ( [DocumentId] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY] </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload