The fist issue is related to primary keys and autoincrement. In the case of primary keys that are int's the database being generated isn't creating these using AUTOINCREMENT (equivalent of Identity for Sql Server) even if specifying it explicitly using the fluent api. The second issue is related to cascade on delete for a one to many relationship. The foreign key relationship in the EntityObject table is not being created with ON DELETE CASCADE even if specifying it explicitly. In both cases when omitting the directives using the fluent api shouldn't the default conventions of StoreGeneratedIdentityKeyConvention and OneToManyCascadeDeleteConvention be followed? Using other data providers (e.g., Sql Server Compact Edition) with the same model gives the correct result.
Code: Select all
public class Entity
{
public Entity()
{
this.Name = "";
}
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection Objects { get; set; }
}
public class EntityConfiguration
: EntityTypeConfiguration
{
internal EntityConfiguration()
{
// Keys/Relationships
this.HasKey(e => e.Id);
this.Property(e => e.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
this.HasMany(e => e.Objects)
.WithRequired()
.HasForeignKey(o => o.EntityId)
.WillCascadeOnDelete();
// Properties
this.Property(e => e.Name).HasMaxLength(200).IsRequired();
}
}
public class EntityObject
{
public int Id { get; set; }
public string Name { get; set; }
public int EntityId { get; set; }
}
public class EntityObjectConfiguration
: EntityTypeConfiguration
{
internal EntityObjectConfiguration()
{
// Keys/Relationships
this.HasKey(o => o.Id);
this.Property(o => o.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
// Properties
this.Property(o => o.Name).HasMaxLength(200).IsRequired();
}
}
CREATE TABLE Entities (
Id INTEGER NOT NULL,
Name varchar(200) NOT NULL,
PRIMARY KEY (Id)
)
CREATE TABLE EntityObjects (
Id INTEGER NOT NULL,
Name varchar(200) NOT NULL,
EntityId int32 NOT NULL,
PRIMARY KEY (Id),
FOREIGN KEY (EntityId) REFERENCES Entities (Id)
)