Note that there are some explanatory texts on larger screens.

plurals
  1. POError Related to FK in One-to-Many Relationship
    primarykey
    data
    text
    <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>
    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. CODo you really have a `Vehicle` table as the exception says? I'm wondering because if you use TPC you should not have a table for the abstract base class, only for the two concrete classes `Car` and `Motorbike`. Also: Do you have a connection string with all the edm metadata stuff in it? I would expect that because apparently you use Model-First. But I'm wondering why you have Fluent API mapping code, which is Code-First and not Model-First.
      singulars
    2. CO@Slauma: 1. Another strange thing - as you mentioned - is that I have a `Vehicle` class, while this does not happen for another abstract base class named `Ad` and its derived classes. 2. My connection string is as follows: <add name="IranCarContext" providerName="System.Data.SqlClient" connectionString="Data Source=localhost;Initial Catalog=IranCar;Integrated Security=SSPI;" /> 3. About Model-First: first I used EDM Designer to model my entities visually. Then I used **EF DbContext Generator** to create POCO classes and `DbContext`. Finally I used EF + Fluent API to create the databse.
      singulars
    3. COPerhaps it would help if you show the full mapping in Fluent API for the classes above, especially also the TPC mapping. Having a navigation property (`Brand`) on the *base class* (`Vehicle`) is a possible problem with TPC: http://weblogs.asp.net/manavi/archive/2011/01/03/inheritance-mapping-strategies-with-entity-framework-code-first-ctp5-part-3-table-per-concrete-type-tpc-and-choosing-strategy-guidelines.aspx (see section "Polymorphic Associations with TPC is Problematic" in this article).
      singulars
 

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