Code first - Cannot insert the value NULL into column 'Id' - entity-framework-4.1

This is my code, very simple...
var newUser = new User();
newUser.Id=id;
newUser.Email = email;
this.DataContext.Set<User>().Add(newUser);
this.DataContext.SaveChanges();
The error I get is a sqlexception at this.DataContext.SaveChanges(); stating that:
Cannot insert the value NULL into column 'Id', table
'xxxxx.dbo.Users'; column does not allow nulls. INSERT fails.
I have debugged and found that there is value in Id & Email in newUser at
this.DataContext.Set<User>().Add(newUser);
If this is the case, how is the value becoming null?
The error stack trace:
[DbUpdateException: An error occurred while updating the entries. See the inner exception for details.]
System.Data.Entity.Internal.InternalContext.SaveChanges() +204
System.Data.Entity.Internal.LazyInternalContext.SaveChanges() +23
System.Data.Entity.DbContext.SaveChanges() +20
I have not been able to understand or solve this....
Would sincerely appreciate any help in this...
Regards
Arnab
Solution
Ok, thanks to Ladislav to point me in the right direction:
Adding the attribute [DatabaseGenerated(DatabaseGeneratedOption.None)] solved the problem.

Referring to this post it seems that entity framework expects by default that you insert into identity column.
To solve this try:
modelBuilder.Entity<BOB>()
.HasKey(p => p.Id)
.Property(p => p.Id)
.StoreGeneratedPattern = StoreGeneratedPattern.None;
builder.Entity<BOB>().MapSingleType().ToTable("BOB");
or decorate your key in the POCO with:
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.None)] //Fixed typo
public Int64 PolicyID { get; set; }

I ran into the same problem, found your question, and then noticed this Entity Framework 4.1 code-first KeyAttribute as non-identity column question ( which has an attribute that solved my problem ).
If you define User as:
public class User
{
[DataMember, Required, Key, DatabaseGenerated(DatabaseGeneratedOption.None)]
public long ID { get; set; }
[DataMember]
public string Email { get; set; }
}
The key here is putting the attributes:
Key
DatabaseGenerated
onto the ID column. Apparently the issue we are fighting is that Entity Framework by default expects to do inserts with keys being identities.

I fixed this by setting StoreGeneratedPattern on the column properties to Computed.
Step by step below:
Open your EDMX (double click in Visual Studio)
Right click on the column that causes the problem, select properties.
Change StoreGeneratedPattern to be Computed.
Hope that helps somebody.

Related

Fluent NHibernate with MySQL with Mono and ASP MVC 3

I'm attempting to configure Fluent NHibernate with Automapping for MySQL and I've run into a problem. I've googled quite a bit and tried many different things, but haven't found a solution yet.
Anyway, here's my NHibernate configuration
return
Fluently.Configure()
.Database(
MySQLConfiguration.Standard.ConnectionString(cs => cs.FromConnectionStringWithKey("Db"))
.Dialect<NHibernate.Dialect.MySQL5Dialect>()
.ShowSql()
)
.Mappings(
x =>
x.AutoMappings
.Add(
FluentNHibernate.Automapping.AutoMap.AssemblyOf<CoreRegistry>()
.Where (t => t.IsDefined(typeof(AutoMap), false))
.Conventions.Setup (c => {
c.Add<NullConvention>();
c.Add<PrimaryKeyConvention> ();
c.Add<TableNameConvention> ();
})
)
).BuildSessionFactory();
Here's my model:
[AutoMap]
public class MyNewTable
{
public virtual int Id {get; set;}
public virtual string Column1 {get; set;}
[Null]
public virtual string Column2 {get; set;}
}
Here's my code attempting to insert the record:
using (var session = sf.OpenSession())
using (var tx = session.BeginTransaction()) {
var myReallyTable = new MyNewTable
{
Column2 = null,
Column1 = "ghrtehrthrtete"
};
session.Save (myReallyTable);
tx.Commit ();
}
Lastly, here is the sql that it is actually generating:
INSERT INTO myproject.Core.MyNewTable, myproject.Core, Version=1.0.4911.33346, Culture=neutral, PublicKeyToken=null (Column1, Column2) VALUES (?, ?)
For some reason it is injecting my project's assembly name before my table name and also appending the assembly version and stuff after it. I've tried setting the default schema to my database name. I tried switching the dialect to MySQLDialect, but that didn't work either.
If anyone's got any light to shine on this I'd be very grateful.
Thanks
Well I get to come back and show what a jackass I am.
I had a table name convention that was using instance.Name instead of instance.EntityType.Name. Oh well, at least this post will be here if anyone wants to know how to AutoMap with MySQL. :D

ejb3 toplink jpa 1.0 querying and sequencing

I have 2 questions:
suppose we have one entity named class and another called student. each class has onetomany students.
public class Clas implements Serializable {
#Id
#GeneratedValue(strategy=GenerationType.SEQUENCE)
private int id;
#OneToMany(cascade=CascadeType.ALL)
Collection<Student> students;
public clas(){
super();
}
..... getters and setters
}
q1: i get the exception there are no fields to be mapped, when adding any other column like String name, it works, but i don't need that field what can i do ?
q2: the ids is autogenerated, and i want to query all students in class c1, but i don't has the id of this class, how to do such query ?
iam working with mysql server glassfish v2.1 toplink jpa 1.0
Thanks
The student class must have a property named 'classID' (or whatever) that refers to the
Clas's id property. That should be annotated like #ManyToOne.
If that's done already by IDE, then check id generation strategy. For example, if you are using mysql, the primary key is auto_increment, then set th id's strategy to
GenerationType.AUTO and recompile. Tell me if any other errors shows up. :) .
ok. I think I understood you question. You may use NamedQueries written in Query Languages dependent on your library (in your case toplink) like EJB QL or HBQL. You can create Session Beans for querying.
public class ClassSessionBean {
#PersistenceContext(unitName="your PU name in persistence . xml")
private Entitymanager em;
publicClas selectByID(int id) throws NoResultException {
Query q = em.createQuery("select class from Class class where class.id=?");
q.setParameter(1, id);
Clas clas = q.getResultList();
return clas;
}
}
Note that the above code may contain syntax errors because I have not checked it anywhere.
Hope you find some help from this :) .

Define relationships for hierarchical data in Entity Framework Code First

I have a hierarchical relationship defined for one of my tables where the relationship is stored in a separate join table. The join table also contains information about the type of relationship. The (simplified) schema looks like:
Bills
ID int IDENTITY(1,1) NOT NULL (PK)
Code varchar(5) NOT NULL
Number varchar(5) NOT NULL
...
BillRelations
ID int IDENTITY(1,1) NOT NULL (PK)
BillID int NOT NULL
RelatedBillID int NOT NULL
Relationship int NOT NULL
...
I have FK relationships defined on BillID and RelatedBillID to ID in the Bills table.
I'm trying to map this in Entity Framework Code First with little success. My classes look like the following. Ignore the RelationshipWrapper stuff, it's a wrapper class around an enum named Relationship that corresponds to the value in the Relationship column.
public class Bill
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int ID { get; set; }
[Required]
[StringLength(5)]
public string Code { get; set; }
[Required]
[StringLength(5)]
public string Number { get; set; }
public virtual ICollection<BillRelation> RelatedBills { get; set; }
...
}
public class BillRelation
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int ID { get; set; }
public long BillID { get; set; }
[ForeignKey("BillID")]
public virtual Bill Bill { get; set; }
public long RelatedBillID { get; set; }
[ForeignKey("RelatedBillID")]
public virtual Bill RelatedBill { get; set; }
public RelationshipWrapper Relationship { get; set; }
...
}
I've tried this in various incarnations, both using explicitly defined relationships via the ModelBuilder on the DbContext and data annotations only, but the above defines what I'm looking for. I'm using collections because the the foreign key properties aren't primary keys.
Using this set up I get an error: Bill_ID column is not defined (or something similar).
If I got the ModelBuilder route using the following, I get an error that "The table BillRelations does not exist in the database."
modelBuilder.Entity<Bill>().HasMany( b => b.RelatedBills )
.WithRequired( r => r.Bill )
.Map( m => m.MapKey( "BillID" ).ToTable( "BillRelations" ) );
modelBuilder.Entity<BillRelation>().HasRequired( r => r.RelatedBill )
.WithRequiredDependent()
.Map( m => m.MapKey( "RemoteBillID" ).ToTable( "BillRelations" ) );
I have been able to make it work by defining only one half of the relationship, Bills -> BillRelations, then using a Join in my repository to fill in an [NotMapped] RelatedBill property on the BillRelations class for each of the related bills in the Bill's RelatedBills collection. I'd rather not do this if I can help it.
The only other solution I've thought of is to model each relationship in a separate table (there are 4 types) and use a standard Bill<->Bill mapping through the join table for each of the 4 relationship types -- again I'd rather not do this if I can avoid it.
Can anyone see what I'm doing wrong or tell me if what I want to do is even possible in EF Code First 4.1?
Just a few ideas:
Your mapping with data annotations doesn't work because EF Code First conventions don't recognize which navigation properties belong together. Obviously you want to associate Bill.RelatedBills with BillRelation.Bill. But because there is a second navigation property BillRelation.RelatedBill refering to the Bill entity as well the AssociationInverseDiscoveryConvention can't be applied to recognize the correct relation. This convention only works if you have exactly one pair of navigation properties on the entities. As a consequence, EF assumes actually three relationships, each with only one exposed end in the model. The relationship where Bill.RelatedBills belongs to assumes a not exposed foreign key on the other side according to EF default naming conventions - which is Bill_ID with underscore. It doesn't exist in the database, hence the exception.
In your Fluent API mapping I would just try to remove ...ToTable(...) altogether. I believe that it is not necessary as the mapping knows anyway which table the foreign key belongs to. This will possibly fix the second error ("Table ... does not exist....")
Your second mapping - the one-to-one relationship - does possibly not work as expected because one-to-one relationships are "usually" mapped with a shared primary key association between the tables in the database. (I am not sure though if shared primary keys are really required by EF.) Because your BillId column seems to be a foreign key which is not a primary key at the same time I would try to map the relationship as one-to-many. Moreover because your foreign key columns are exposed as properties in the model you should use HasForeignKey instead of MapKey:
modelBuilder.Entity<Bill>()
.HasMany( b => b.RelatedBills )
.WithRequired( r => r.Bill )
.HasForeignKey ( r => r.BillID );
// turn off/on cascade delete by chaining
// .WillCascadeOnDelete(true/false)
// here at the end
modelBuilder.Entity<BillRelation>()
.HasRequired( r => r.RelatedBill )
.WithMany()
.HasForeignKey ( r => r.RelatedBillID );
// turn off/on cascade delete by chaining
// .WillCascadeOnDelete(true/false)
// here at the end
Edit
It is possible that the whole Fluent mapping is not necessary if you put the [InverseProperty] attribute on one of the navigation properties - for example in your Bill class:
[InverseProperty("Bill")]
public virtual ICollection<BillRelation> RelatedBills { get; set; }
In this attribute you specify the name of the associated navigation property on the related entity. This binds Bill.RelatedBills and BillRelation.Bill together to a pair of navigation properties being the ends of the same association. I hope that EF will do the correct thing with the remaining navigation property BillRelation.RelatedBill, i.e. create a one-to-many relationship - I hope... If the default cascading delete won't work for you, you are forced to use Fluent API though since there is no data annotation attribute to configure cascading delete.

Code first TPT and cascade on delete

I'm using EF4.1 with Code first and TPT (Table per Type) inheritance. I have a structure like this
public class Customer
{
public virtual ICollection<Product> Products {get; set;}
}
public class Product
{
[Required]
public int Id { get; set; }
[Required]
public virtual Customer {get; set;}
public decimal Price { get; set; }
}
public class SpecializedProduct : Product
{
public string SpecialAttribute { get; set; }
}
when i delete a customer i want all the products associated with that customer to be deleted. I can specify a WillCascadeOnDelete(true) between the Customer and the Product:
modelBuilder.Entity<Customer>().HasMany(e => e.Products).WithRequired(p => p.Customer).WillCascadeOnDelete(true);
but since there's a foreighn key relationship between SpecializedProduct and Product i get an Exception when I try to delete the Customer:
The DELETE statement conflicted with the REFERENCE constraint "SpecializedProduct _TypeConstraint_From_Product_To_SpecializedProduct". The conflict occurred in database "Test", table "dbo.SpecializedProduct", column 'Id'. The statement has been terminated.
If i manually set a on delete cascade on the SpecializedProduct _TypeConstraint_From_Product_To_SpecializedProduct constraint it works, but i would like to be able to specify this using the modelbuilder or some other way in code. Is this possible?
Thanks in advance!
Best Regards
Simon
When it comes to database, a TPT inheritance is implemented with a Shared Primary Key Association between the base class (e.g. Product) and all the derived classes (e.g. SpecializedProduct). Now, when you delete a Customer object without fetching its Products property, EF has no idea that this Customer has a bunch of products that also needs to be deleted as per your requirement. If you enable cascade deletes by marking your customer-product association as required, then database will take care of deleting the child record(s) from the product table but if this child record is a SpecializedProduct then the related row on the SpecializedProduct won't get deleted and hence the exception that you are getting. So basically the following code won't work:
// This works only if customer's products are not SpecializedProduct
Customer customer = context.Customers.Single(c => c.CustomerId == 1);
context.Customers.Remove(customer);
context.SaveChanges();
This code will cause EF to submit the following SQL to the database:
exec sp_executesql N'delete [dbo].[Customer] where ([CustomerId] = #0)',N'#0 int',#0=1
That said, There is no way to enable the cascade deletes between Product and SpecializedProduct tables, that's just how EF Code First implements a TPT inheritance and you cannot override it.
So what's the solution?
One way is what you already figured out, manually switching the cascades on between Product and SpecializedProduct tables to avoid the exception when you deleting a customer with SpecializedProducts.
The second way is to let EF take care of the customer's SpecializedProducts when you removing the customer. Like I said before, this happens because the Customer object has not been properly fetched, and EF has no knowledge of customer's SpecializedProducts which means by fetching the customer object properly, Ef will start tracking the customer's associations and will submit necessary SQL statements to make sure that every related record is removed before removing the customer:
Customer customer = context.Customers
.Include(c => c.Products)
.Single(c => c.CustomerId == 1);
context.Customers.Remove(customer);
context.SaveChanges();
As a result, EF will submit the following SQL statements to the database which perfectly removes everything in order:
exec sp_executesql N'delete [dbo].[SpecializedProduct] where ([Id] = #0)',N'#0 int',#0=1
exec sp_executesql N'delete [dbo].[Product] where (([Id] = #0) and ([Customer_CustomerId] = #1))',N'#0 int,#1 int',#0=1,#1=1
exec sp_executesql N'delete [dbo].[Customer] where ([CustomerId] = #0)',N'#0 int',#0=1

Entity Framework - Insert Into Related Tables

I am new to Entity Framework and hence this question may seem a little noobish.
I will try to explain my scenario with he Department-Employee example I have two tables "Department" and "Employee". Department has an identity column DeptID. I am trying to create a new Department and add newly created Employees to it all in one go. Below is my code:
using (MyDB context = new MyDB())
{
Department dept = new Department();
dept.Name = "My Department";
Employee emp = new Employee();
emp.Name = "Emp Name";
emp.Department = dept; //Tried dept.Employees.Add(emp) also, same result
context.AddObject("Department", dept);
context.SaveChanges()
}
But for some reason, the record doesn't get inserted. It throws an error in the second insert query.
Below are the queries:
INSERT INTO Department
(Name)
VALUES ('Dept1' /* #gp1 */);
SELECT ID
FROM Department
WHERE row_count() > 0
AND `ID` = last_insert_id()
--------------------------
INSERT INTO Employee
(DeptID,
Name)
VALUES (19,
'Name'); /* #gp1 */
SELECT id
FROM Employee
WHERE row_count() > 0
AND `id` = last_insert_id()
The error it throws is at line 4 of second query. So I am guessing something is wrong with the Identity thing. I am using MySQL.
Can anyone please explain what could be wrong?
EDIT: I have modified the SQL to suit this example. I can't give my real table details.
What is the structure of your classes? I'd assume there is something slightly wrong and EF isn't correctly building the model.
Also I had some problems with EF4.1 until I manually defined the key. The part of EF that 'assumes' which variable is your key doesn't seem to work on some complex objects like objects that are derived from a base class and also fails in other cases.
Here is what I would expect your code to look like:
public class Department
{
[Key]
public Int64 DepartmentId { get; set;}
public String Name { get; set;}
}
public class Employee
{
[Key]
public Int64 EmployeeId { get; set;}
public String Name { get; set;}
//Adding virtual here allows lazy loading of department
public virtual Department Department {get; set;}
}
public class MyDatabase : DbContext
{
DbSet<Department> Departments;
DbSet<Employee> Employees;
}
I have an entire project at work that relies on Entity framework correctly mapping the above into a Many to One relationship and I've had no issues using code just as shown.
'Name' /* #gp1 */
is missing a close parenthesis. i've added it here.
'Name' ) /* #gp1 */