Why does Fluent NHibernate AutoMappings add an underscore to Id (e.g. Entity_id)?
Hi using fluent nibernate automappings
to map this
public virtual int Id { get; set; }
/*...snip..*/
public virtual MapMarkerIcon MapMarkerIcon { get; set; }
}
to this
CREATE TABLE [Attraction](
[Id] [int] IDENTITY(1,1) NOT NULL,
[MapMarkerIconId] [int] NULL
)
with this:
var cfg = Fluently.Configure()
.Database(MsSqlConfiguration.MsSql2005.ConnectionString(connectionString)
.DefaultSchema("xxx"))
.Mappings(m =>
{
m.AutoMappings
.Add(
AutoMap.AssemblyOf<Partner>().Where(
n => n.Namespace == "xxx.Core.Domain")
);
m.FluentMappings.Conventions.Add(PrimaryKey.Name.Is(x => "Id"),
DefaultLazy.Always(),
ForeignKey.EndsWith("Id")
);
开发者_StackOverflow社区 }
)
.ExposeConfiguration(c => c.SetProperty(Environment.ReleaseConnections, "on_close"))
.ExposeConfiguration(c => c.SetProperty(Environment.ProxyFactoryFactoryClass, typeof(ProxyFactoryFactory).AssemblyQualifiedName))
.BuildConfiguration();
Why do I get
Server Error in '/XXX.Web' Application. Invalid column name 'MapMarkerIcon_id'.
How can I make fluentnibernate use MapMarkerIconId instead of MapMarkerIcon_id?
You need an Id convention.
See http://wiki.fluentnhibernate.org/Available_conventions and http://wiki.fluentnhibernate.org/Convention_shortcut
None of the above links work. Here's the solution with links to current Fluent NHibernate & automapping documentation.
The issue (a simple example):
Say you have the simple example (from fluent's wiki) with an Entity and it's Value Objects in a List:
public class Product
{
public virtual int Id { get; set; }
//..
public virtual Shelf { get; set; }
}
public class Shelf
{
public virtual int Id { get; set; }
public virtual IList<Product> Products { get; set; }
public Shelf()
{
Products = new List<Product>();
}
}
With tables which have e.g.
Shelf
id int identity
Product
id int identity
shelfid int
And a foreign key for shelfid -> Shelf.Id
You would get the error: invalid column name ... shelf_id
Solution:
Add a convention, it can be system wide, or more restricted.
ForeignKey.EndsWith("Id")
Code example:
var cfg = new StoreConfiguration();
var sessionFactory = Fluently.Configure()
.Database(/* database config */)
.Mappings(m =>
m.AutoMappings.Add(
AutoMap.AssemblyOf<Product>(cfg)
.Conventions.Setup(c =>
{
c.Add(ForeignKey.EndsWith("Id"));
}
)
.BuildSessionFactory();
Now it will automap the ShelfId
column to the Shelf
property in Product
.
More info
Wiki for Automapping
Table.Is(x => x.EntityType.Name + "Table")
PrimaryKey.Name.Is(x => "ID")
AutoImport.Never()
DefaultAccess.Field()
DefaultCascade.All()
DefaultLazy.Always()
DynamicInsert.AlwaysTrue()
DynamicUpdate.AlwaysTrue()
OptimisticLock.Is(x => x.Dirty())
Cache.Is(x => x.AsReadOnly())
ForeignKey.EndsWith("ID")
See more about Fluent NHibernate automapping conventions
精彩评论