Fluent Nhibernate - How to specify table name - c#

I just started learning Nhibernate and Fluent Nhibernate. I want to specify table name when I create my mapping class.
Entity name in application is "CustomerOrders" but in the oracle database, table name is "CUSTOMER_ORDERS".
I learnt from googling that I can use "WithTable" to specify database table name.
I am not sure how to use it and where as Vs2008 didn't find the method.
Thanks

public class CustomerOrdersMap : IAutoMappingOverride<CustomerOrders>
{
public void Override(AutoMapping<CustomerOrders> mapping)
{
mapping.Table("CUSTOMER_ORDERS");
}
}

WithTable was renamed to Table for the 1.0 release. It was mentioned in the release notes (first bullet point).

Related

EF6 db first pluralize/singularize for table names that contain underscores

I am trying to create a new Entity Data Model (database first) with entity framework 6 using the wizard provided in Visual Studio 2015 Community edition. All my table names are pluralized (ex. mn_Bills). I chose the "Pluralize or singularize generated object names" but this does not singularizes the object names (i.e. the generated class for table mn_Bills is mn_Bills in a mn_Bills.cs file).
I have modified the "Name" and "Entity Set Name" in the Model Browser which resolved only partly the problem. The classes generated are singular, which is good, but the foreign key associations for N to 1 associations are pluralized:
public partial class mn_Bill
{
...
public virtual mn_Clearing mn_Clearings { get; set; }
}
Do you know if there is any workaround for this problem (besides changing the table names)?
I met a similar problem with underscore table names and it seems that the PluralizationService doesn't support underscores.
The answer, it seems, is to override the default service with some of your own code.
I've not done this for a database first edmx solution, because I'm using an alternative code generation engine - but this may be possible to override in the code generation step.
See this answer here:
Entity Framework EDMX - Entity Pluralization Fix
https://blogs.msdn.microsoft.com/efdesign/2008/12/01/pluralization/
Just select "Pluralize or singularize" option in the EDMX wizard.

How to map table names and column name different from model in onmodelcreating method in Entity Framework -6?

I have my database with table names starting with "tbl" prefix and column names like "ua_id" which are understandable in context of project but problematic if used in a model i.e names should be readable or meaningful(not like indicative names defined in database).
So I want to map them in my onmodelcreating method but I have no idea about it. I studied it in following blog:
http://weblogs.asp.net/scottgu/entity-framework-4-code-first-custom-database-schema-mapping
but this is for EF 4.1 and method doesn't work for EF 6.(mapsingletype method)
I want to map my tables by columns to my model as I can't change the column names. I just want the newer version of that syntax in the blog.
Thank You.
If you are using Code First, you can simply decorate your model with Table and Column attribute and give it the database names.
[Table("tbl_Student")]
public class Student
{
[Column("u_id")]
public int ID { get; set; }
}
These attributes are available in the System.ComponentModel.DataAnnotations.Schema namespace
You can keep following inside OnModelCreating
modelBuilder.Entity<MyModel>()
.Property(e => e.MyColumn).HasColumnName("DBColumn")
And if you're not using code-first, just select a table in the model diagram, hit F4 (properties) and change the name.

Why does EF 5.x use plural name for table?

I've had some experiences in ORM framework such as Hibernate, even Entity Framework 3.0.
By default, those frameworks use singular name for table. For example, class User will map to table User.
But when I migrate to EF 5.x by using Visual Studio 2012, it uses plural name and causes many errors unless I manually map that class by using TableAttribute as:
[Table("User")]
public class User {
// ...
}
Without TableAttribute, if I have a DbContext as below:
public CustomContext : DbContext {
// ...
public DbSet<User> Users { get; set; }
}
Then calling:
var list = db.Users.ToList();
The generated sql looks like:
Select [Extent1].[Username] From [Users] as [Extent1]
Therefore, the error will occur because I don't have any table named Users. Futhermore, I refer to name tables in singular form to plural form. You can see why in this link
I wonder why Microsoft implement EF 5.x that way rather than same to EF 3.0?
Updated:
We can tell EF not use pluralized name by using this code in the context class:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
base.OnModelCreating(modelBuilder);
}
Even if EF tool generates pluralized name, it should keep the mappings to the original database, shouldn't it? Because some users might want to change a few name of fields on their class. EF 3.0 works well, but EF 5.x does not.
Guys, can you give my a reason, please!
That convention is defined in the PluralizingTableNameConvention convention defined by default in the DbModelBuilder.Conventions
If you want to exclude it from all tables, you can use the code from this question.
Why this is done the way it is, I do not know, but I must say that I am, personally, in the camp that thinks that table names should be pluralized :)
Of the example databases provided with an SQL Server instance, Northwind has plurals, and AdventureWorks uses a singular form, so at best, there is no established standard. I've had quite a few discussion for or against each one, but one thing that everybody can agree on is that when once pick a naming strategy, you should stick with it.
When you generate your model with entity framework, uncheck "Pluralize or singularize generated object names"

Invalid object name 'dbo.TableName' when retrieving data from generated table

I'm using entity framework code first to create my tables. Please note - create the tables, not the DB, since I'm working on a hosted environment and I don't have a user that is allowed to create db's.
Committing a DB update works fine, but retrieving data gives the exception:
Exception Details: System.Data.SqlClient.SqlException: Invalid object name 'dbo.EventHosts'.
I've read that it happens because I'm not using EF Code First to create the DB. That's fine, but how do I elegantly solve this?
All the generated tables do not have a prefix like dbo. A solution like this doesn't work, and isn't elegant at all:
[Table("EventHosts", Schema = "")]
Ok, for me issue was that I had a table called dbo.UserState and in C# EF was trying to access dbo.UserStates because of pluralization.
The solution was to put Table attribute above class and specify the exact table name:
[Table("UserState")]
public class UserState
{
[Key]
public int UserId { get; set; }
}
To answer your first question: use the schema created for you by your hosting provider.
To answer your second question: No there is currently no direct way to change the default schema globally because you cannot modify existing conventions or create new conventions. You can try to hack it.
For example you can override OnModelCreating and use reflection to get all DbSet<> properties declared in your context. Than you can just use simple loop on these properties and create ToTable mapping call with name of the property as table name and your custom schema. It will require some playing with reflection to make this work.
Alternatively you can try to do some reusable approach by implementing custom conventions. You can find many different articles about using your own conventions with EF. Some examples:
Custom Conventions in Entity Framework Code First v 4.1
Conventions in Entity Framework 4.1 Final
My high level untested idea is following same principle and create assembly level attribute which will be processed by the convention mechanism and applied on all your entities.
Try to set default schema name to 'dbo' in SQL SERVER.
http://msdn.microsoft.com/en-us/library/ms173423.aspx
On of the reason for this error is the table named "EventHosts" may not Exist or that table is renamed to some other name please check with that..
https://stackoverflow.com/a/12808316/3069271
I had same issue, it was pluralize problem between mapping and db.

Help making Fluent NHibernate create an oracle sequence for each table

I am using Fluent NHibernate's (1.0 RTM) automapping feature to create my oracle database schema. My issue is that all the tables are using a single "hibernate-sequence", whereas I would prefer (and my boss would demand) a sequence generator for each table.
Any ideas?
Managed to solve my own solution. Here's the code:
public class OraclePrimaryKeySequenceConvention : IIdConvention
{
public void Apply(IIdentityInstance instance)
{
instance.GeneratedBy.Sequence(string.Format("Sequence_{0}",
instance.EntityType.Name));
}
}
Yay. :-)

Categories

Resources