MikeW
MikeW

Reputation: 4809

Insert null fails with Model First EF

I'm using a context generated from an EDMX for a mvc3 webapp. I'm getting a NULL insert fails error on an entity

[Serializable]
[DataContract(IsReference = true)]
[EdmEntityType(NamespaceName = "Model", Name = "Thing")]
public class Thing: EntityObject
{
    public RolloverEntry();

    [DataMember]
    [EdmScalarProperty(EntityKeyProperty = true, IsNullable = false)]
    public int id { get; set; }
    [SoapIgnore]
    [EdmRelationshipNavigationProperty("Model", "FK_ThingStep1", "Step1")]
    [DataMember]
    [XmlIgnore]
    public EntityCollection<Step1> Step1 { get; set; }
    [SoapIgnore]
    [EdmRelationshipNavigationProperty("Model", "FK_ThingStep2", "Step2")]
    [XmlIgnore]
    [DataMember]
    public EntityCollection<Step2> Step2 { get; set; }

    public static Thing CreateThing(int id);
}

Data access to other parent-child relationships are working and persisted correctly - I can't seem to find what's wrong with this table tho - any ideas appreciated

Exception Recieved:

{"Cannot insert the value NULL into column 'id', table 'myapp.dbo.Thing'; column does not allow nulls. INSERT fails.\r\nThe statement has been terminated."}

Thanks

Upvotes: 0

Views: 226

Answers (1)

JayC
JayC

Reputation: 7141

I'm guessing you need some sort of hint in your model that the database should generate the ids for the id column. You might want to see if StoreGeneratedPattern is set to Identity for your model property id or something along those lines.

Upvotes: 1

Related Questions