Note that there are some explanatory texts on larger screens.

plurals
  1. POError Related to FK in One-to-Many Relationship
    text
    copied!<p>I am using EF 5 Beta 2 Code-First. I have created an edmx file which has 2 entities among others named <em>Brand</em> and <em>Vehicle</em>.</p> <p>One <em>Brand</em> can have zero or more (many) <em>Vehicles</em> and every <em>Vehicle</em> should have a <em>Brand</em> (required). <em>Vehicle</em> has a foreign key named <em>BrandID</em> which is <em>non-nullable</em>.</p> <pre><code>(relationship) Brand +-------------------&gt; Vehicle (1) (*) </code></pre> <p>Also I did use <em>EF 5 DbContext Generator</em> to create POCO classes.</p> <h2>The Problem</h2> <p>When I try to either Read or Write records, I get the following error:</p> <p><strong>error 3023: Problem in mapping fragments starting at line 155:Column Vehicle.BrandID in table Vehicle must be mapped: It has no default value and is not nullable.</strong></p> <p>Notice: I am using <strong>TPC</strong> inheritance mapping where Vehicle is an <em>abstract base class</em> from which 2 classes (<em>Car</em> &amp; <em>Motorbike</em>) are being derived.</p> <p>Here is class definition plus related fluent API code:</p> <pre><code>//------------ Class definiions --------------- public abstract partial class Vehicle { public int VehicleID { get; set; } public short BrandID { get; set; } public virtual Brand Brand { get; set; } public partial class Car : Vehicle { public string BodyType { get; set; } } public partial class Motorbike : Vehicle { } public partial class Brand { public Brand() { this.Vehicles = new HashSet&lt;Vehicle&gt;(); } public short BrandID { get; set; } public string Name { get; set; } public virtual ICollection&lt;Vehicle&gt; Vehicles { get; set; } } //--------------- Fluent API code --------------- modelBuilder.Entity&lt;Vehicle&gt;() .Property(p =&gt; p.VehicleID) .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity); modelBuilder.Entity&lt;Car&gt;() .Map(m =&gt; { m.ToTable("Car"); m.MapInheritedProperties(); }); modelBuilder.Entity&lt;Motorbike&gt;() .Map(m =&gt; { m.ToTable("Motorbike"); m.MapInheritedProperties(); }); modelBuilder.Entity&lt;Brand&gt;() .HasMany(b=&gt;b.Vehicles) .WithRequired(v=&gt;v.Brand) .HasForeignKey(p =&gt; p.BrandID); modelBuilder.Entity&lt;Brand&gt;() .Property(p =&gt; p.BrandID) .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity); </code></pre> <p>This is so strange since everything seems OK and has been checked several times.</p> <p>Any thoughts would be greatly appreciated.</p>
 

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