Fluent NHibernate with MySQL with Mono and ASP MVC 3 - mysql

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

Related

Intersect or Except methods in linq not working in MYSQL

I am using entity framework with mysql. I have to use Intersect in one of the query but it throws the error "Specified method is not supported.". Can anyone suggest alternatives other than taking data to memory and apply Intersect on that.
public List<share> GetFeed(List<long> UserRules, List<long> ListedShares, string AuthorID)
{
List<share> listshare = DbContext
.shares
.Where(x => UserRules.Intersect(x.share_rules.Select(y => y.rule_id)).Any())
.ToList();
return listshare;
}
Specified method is not supported. means that EntityFramework is not able to translate it to SQL query. Actually, in most cases, it is possible to convert it to Contains, which is supported. You should change your method to match:
public List<share> GetFeed(List<long> UserRules, List<long> ListedShares, string AuthorID)
{
List<share> listshare = DbContext
.shares
.Where(x => x.share_rules.Any(y => UserRules.Contains(y.rule_id)))
.ToList();
return listshare;
}

Using Find in CodeFirst (EntityFramework) to get non-primary keys

My understanding is that find only takes the primary key as the parameter. That works great if the value you are looking for is actually the primary key. In my case, I have a class like this:
public class Chamber
{
[Key]
public int Id {get;set;}
public string ChamberName { get; set; }
}
I want to check whether a given ChamberName exists in either my context or the database itself. How can I do that? Do I have to somehow enumerate of the context myself first, then, look it up in the database with a call like db.Chambers.where(a=>a.ChamberName.equals...?
I can see it working well if ChamberName is my primary key, but it is not.
THanks,
There is a property called Local in the DbSet. You can query that first to find entities loaded to the context.
var entity = db.Chambers.Local.Where(/**/).SingleOrDefault();
if (entity == null)
{
entity = db.Chambers.Where(/**/).SingleOrDefault();
}
You can't use the .Find() method - but how about:
public Chamber FindByChamberName(string chamberName)
{
using(MyDbContext ctx = new MyDbContext())
{
Chamber result = ctx.Chambers
.FirstOrDefault(c => string.Compare(c.ChamberName, chamberName, true));
return result;
}
}
You don't have to manually enumerate anything - just retrieve the first occurence of a chamber by that name - or none.
If you just need to know whether a given chamber (specified by its ChamberName) exists or not, you could use the .Any() method in Linq:
using(MyDbContext ctx = new MyDbContext())
{
return ctx.Chambers.Any(c => string.Compare(c.ChamberName, chamberName, true));
}

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

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.

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 */

Can I update 1 object only with Linq to SQL?

Its a simple question, but I'm not aware of the answer and I couldn't get it to work.
Can I update only one entity on the entire DataContext? Or should I follow plain ADO.NET for this operation only?
Edit:
public MyObject GetMyObjectById(int selectedId)
{
DataContext db = _dbManager.GetContext();
return db.MyObject.SingleOrDefault(p => p.Id == selectedId);
}
I am getting an object with the above query...
I am querying then for an integer...on another table/object
public int GetMyInteger()
{
DataContext db = _dbManager.GetContext();
return db.MyAnotherObject.FirstOrDefault().MyInteger;
}
Everything is fine for all my operations...but now i just want to update only the integer i got from the database...
public void SetMyInteger(int updInteger)
{
DataContext db = new DataContext(ConnectionString);
MyAnotherObject theEntity = db.MyAnotherObject.FirstOrDefault();
atheEntity.MyInteger = updInteger;
db.SubmitChanges(ConflictMode.ContinueOnConflict);
}
The above method deleted MyObject i got from the first query!!! Of course if i use the static context DataContext tries to update MyObject and MyAnotherObject which seems the correct behaviour.
Edit:
I have changed the method getting the integer with a new datacontext as well and seems to working correctly, i have a strange thought on why called the delete method, because it was the method that was called, but again .. is working now...
Thank you all for your time.
Yes it's possible. What have you tried? It should be as simple as this:
using (var dc = new YourDataContext())
{
Person p = dc.Persons.Take(1).Single();
p.FirstName = "Ahmad";
dc.SubmitChanges();
}
Yes, you can:
Foo foo = dc.Foos.Where(foo => foo.Id == 345).Single();
foo.Name = "foo";
dc.SubmitChanges();