I'm using fluent nhibernate, I would turn on delete cascade. but I do not work. it only deletes the foregin key.
The following configuration fluent nhibernate:
public virtual void TreatConfiguration(NHibernate.Cfg.Configuration configuration)
{
var update = new SchemaUpdate(configuration);
update.Execute(false, true);
}
public ISessionFactory CreateSessionFactory(string istanza, string db)
{
//string server = #"BRUX-PC\SQLEXPRESS";
//int port = 1433;
string server = istanza;
string database = db;
const string user = "xxxx";
const string password = "xxxx";
var connectionString = string.Format("Server={0};Database={1};User Id={2};Password={3};",
server, database, user, password);
var autoMap = AutoMap.AssemblyOf<AggregateBase>()
.Where(t => typeof (AggregateBase).IsAssignableFrom(t))
.Conventions.Add(
ConventionBuilder.HasMany.Always(x => x.Cascade.AllDeleteOrphan()),
PrimaryKey.Name.Is(o => "Id"),
ForeignKey.EndsWith("Id"),
DefaultLazy.Never(),
DefaultCascade.All(),
DynamicUpdate.AlwaysTrue(),
DynamicInsert.AlwaysTrue()
);
return Fluently.Configure()
.Database(
MsSqlConfiguration.MsSql2008.ConnectionString(connectionString))
.Mappings(m => m.AutoMappings.Add(autoMap))
.ExposeConfiguration(TreatConfiguration)
.BuildSessionFactory();
}
my controller :
contabilitaRepository.RemoveByIdFattura(testataContabilita.IdFattura);
my repository:
public void RemoveByIdFattura(Guid? id)
{
var userToDelete =
repository.Single(c => c.IdFattura == id);
repository
.Remove(userToDelete);
}
my model:
public class TestataContabilita : AggregateBase
{
public virtual Guid IdFattura
{
get;
set;
}
public virtual int NumeroRegistrazione
{
get;
set;
}
public virtual string TipoVendita
{
get;
set;
}
public virtual IList<CorpoContabilita> CorpoContabilita { get; set; }
}
public class CorpoContabilita : AggregateBase
{
public virtual int NumeroRegistrazione
{
get;
set;
}
public virtual int? ControPartita
{
get;
set;
}
public virtual string Automatico
{
get;
set;
}
}
when I run the elimination, I only deletes "TestataContabilita" while the "CorpoContabilita" remains, but only deletes the foregin key. why?
You may have to do
ConventionBuilder.HasMany.Always(x => x.Cascade.AllDeleteOrphan().Inverse()
in the mapping
Also you have to do
TestataContabilita.CorpoContabilita.Clear();
to remove the items from the collection.
I've tried to reproduce your case. I implemented some kind of a repository and your mapping works just it supposed to. Deletes testataContabilita object with collection of CorpoContabilita.
But I accidentally made a several mistakes in a repository at first - and got null in foreign keys too. In fact they were null even before deleting testataContabilita - due to errors in implementation.
So, what repository implementation do you use? How do you work with Session and Transaction objects? On what object you perform Save? May be you can upload your project somewhere with full implementation.
Related
Suppose I have only two classes: Group and User. User has groups and Group has members (instance of users)
public class User {
public virtual int id { set; get; }
public virtual string username { set; get; }
public virtual IList<Group> groups { set; get; }
public User()
{
groups = new List<Group>();
}
public virtual void joinGroup(Group group)
{
if (this.groups.Contains(group))
throw new AlreadyJoinedException();
group.members.Add(this);
this.groups.Add(group);
}
public class Group
{
public virtual int id { set; get; }
public virtual string name { set; get; }
public virtual User administrator { set; get; }
public virtual IList<User> members { set; get; }
public Group()
{
members = new List<User>();
}
As you can see the domain it's quite simple. I've already mapped both classes correctly using Fluent NHibernate,
public class UserMapping : ClassMap<User>
{
public UserMapping()
{
this.Id(user => user.id).GeneratedBy.Identity();
this.Map(user => user.username).Not.Nullable().Length(50).Not.LazyLoad();
this.HasManyToMany(user => user.groups).Table("MemberPerGroup").ParentKeyColumn("id_user").ChildKeyColumn("id_group").Not.LazyLoad();
}
}
public class GroupMapping : ClassMap<Group>
{
public GroupMapping()
{
this.Id(group => group.id).GeneratedBy.Identity();
this.Map(group => group.name).Not.Nullable().Length(50).Not.LazyLoad();
this.References(group => group.administrator).Not.Nullable().Not.LazyLoad();
this.HasManyToMany(group => group.members).Table("MemberPerGroup").ParentKeyColumn("id_group").ChildKeyColumn("id_user").Not.LazyLoad();
}
}
I'm progamming a web application using ASP MVC 4. My problem shows up when a user tries to join group. It doesn't break but it neither works fine (doesn't insert into the table the new row in MemberPerGroup). I'm doing something like it:
public void JoinGroup(User user,Group group){
this.userRepository.GetSessionFactory().TransactionalInterceptor(() =>
{
user.joinGroup(group);
});
}
Thanks in advance.
Ivan.
It seems your mapping has no cascading set?
this.HasManyToMany(group => group.members)
.Table("MemberPerGroup")
.ParentKeyColumn("id_group")
.ChildKeyColumn("id_user")
.Not.LazyLoad()
.Cascade.SaveUpdate();
I'm curious - why do you use GetSessionFactory()? our repositories take an ISession object in the constructor, (injected by autofac, but that's irrelevant) from which we start our queries:
// even better to use a transaction, but this is just a sample
_session.SaveOrUpdate(user);
_session.Flush();
I had to define a recursive relationship on a composite key. After much trials, I ended up with this:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<Category>()
.Property(t => t.WhichAmazon).HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
modelBuilder.Entity<Category>()
.Property(t => t.IdCategory).HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
modelBuilder.Entity<Category>()
.HasKey(c => new {c.WhichAmazon, c.IdCategory})
.HasOptional(p => p.Children)
.WithMany()
.HasForeignKey(c => new { c.WhichChildrenAmazon, c.ChildrenId });
}
for this table
public class Category
{
// Keys and relationships defined in BooksDataLayer
[MaxLength(2)]
public string WhichAmazon { get; set; }
public int IdCategory { get; set; }
public string Name { get; set; }
[Timestamp]
public Byte[] TimeStamp { get; set; }
public DateTime LastCheck { get; set; }
public virtual List<Book> Books { get; set; }
public string WhichChildrenAmazon { get; set; }
public int? ChildrenId { get; set; }
public virtual List<Category> Children { get; set; }
}
While trying to Add-Migration I was constantly having the same error: "Sequence contains no elements". As I was "almost" sure this definition was right, I went ahead a re-created a new Db, WITHOUT migration. Was perfectly OK, no problems with the Db at all. So there is "something" in there which EF 6 does not like. I had a confirmation, as EF power tools bombs if I try to get a schema "Exception has been thrown by the target of an invocation".
I'll see what happens now with migration if I restart from there, but I am afraid to not be able to use anymore with this Db. I do like the tool, a lot, so I hope this can be fixed.
The issue is an invalid configuration of the relationship.
The Fluent API call includes this:
.HasOptional(p => p.Children)
.WithMany()
This is invalid because Children is a collection navigation. The correct config is:
.HasMany(p => p.Children)
.WithOptional()
We are planning to take a fix to provide a better exception message post-EF6.
Opened a bug for this on the EF codeplex site: http://entityframework.codeplex.com/workitem/1015
I am trying to create my own foreign key convention that will name the FK in "FK_SourceTable_TargetTable" format.
However, when I run it I end up with two foreign keys instead of one.
My custom foreign key convention looks like this:
public class OurForeignKeyConvention : ForeignKeyConvention
{
protected override string GetKeyName(Member property, Type type)
{
if (property == null)
return string.Format("FK_{0}Id", type.Name); // many-to-many, one-to-many, join
if (property.Name == type.Name)
return string.Format("FK_{0}_{1}", property.DeclaringType.Name, type.Name);
return string.Format("FK_{0}_{1}_{2}", property.DeclaringType.Name, property.Name, type.Name);
}
}
My code to exercise it:
[TestMethod]
public void ShouldBeAbleToBuildSchemaWithOurConventions()
{
var configuration = new Configuration();
configuration.Configure();
Fluently
.Configure(configuration)
.Mappings(m => m.FluentMappings
.AddFromAssemblyOf<Widget>()
.Conventions.Add<OurForeignKeyConvention>()
)
.BuildSessionFactory();
new SchemaExport(configuration).Create(false, true);
}
My classes and mappings:
public class Widget
{
public virtual int Id { get; set; }
public virtual string Description { get; set; }
public virtual WidgetType Type { get; set; }
public virtual ISet<WidgetFeature> Features { get; set; }
}
public class WidgetFeature
{
public virtual int Id { get; set; }
public virtual Widget Widget { get; set; }
public virtual string FeatureDescription { get; set; }
}
public class WidgetMap : ClassMap<Widget>
{
public WidgetMap()
{
Id(w => w.Id);
Map(w => w.Description);
HasMany(w => w.Features).Cascade.AllDeleteOrphan().Inverse();
}
}
public class WidgetFeatureMap : ClassMap<WidgetFeature>
{
public WidgetFeatureMap()
{
Id(w => w.Id);
Map(w => w.FeatureDescription);
References(w => w.Widget);
}
}
The end result is two foreign keys, one called what I want - FK_WidgetFeature_Widget - and another one called FK_WidgetId.
If I change OurForeignKeyConvention to always return the same name regardless of whether the "property" parameter is null then I correctly get a single FK - but I then cannot get the "SourceTable" part of my FK name.
Can anyone explain what I am doing wrong here? Why is GetKeyName called twice? And why does one of the calls not provide a value for the "property" parameter?
Doh. ForeignKeyConvention provides the name for the FK column. What I should have been using is the IHasManyConvention, which can be used to name the FK constraint itself.
public class OurForeignKeyConstraintNamingConvention : IHasManyConvention
{
public void Apply(IOneToManyCollectionInstance instance)
{
instance.Key.ForeignKey(string.Format("FK_{0}_{1}", instance.Relationship.Class.Name, instance.EntityType.Name));
}
}
I am new to using Fluent NHibernate and NHibernate for the first time. I've used a custom written mapper since about 2000 that was written in house. Made a switch to LinqToSQL about 2 years ago, and about 6 months ago to Entities.
I'd like to see what Fluent/NHibernate have to offer. However, I can't seem to get it to run correctly. The following is a copy of my classes, their references, the ClassMaps. Can someone tell me if this simple implementation is correct?
This is my mappings and object classes:
using System;
using FluentNHibernate.Mapping;
namespace MyData.Data.Mappings
{
public class Login
{
public virtual int LoginId { get; private set; }
public virtual string Username { get; set; }
public virtual User User { get; set; }
}
public class LoginClassMap : ClassMap<Login>
{
public LoginClassMap()
{
Table("Logins");
Id(d => d.LoginId).GeneratedBy.Guid();
Map(d => d.Username).Not.Nullable().Length(50);
HasOne(d => d.User).ForeignKey("UserId").Cascade.All();
}
}
public class User
{
public virtual Guid UserId{ get; private set; }
public virtual string FirstName { get; set; }
public virtual string LastName { get; set; }
public virtual Login Login { get; set; }
}
public class UserClassMap : ClassMap<User>
{
public UserClassMap()
{
Table("Users");
Id(d => d.UserId).GeneratedBy.Guid();
Map(d => d.FirstName).Not.Nullable().Length(100);
Map(d => d.LastName).Not.Nullable().Length(100);
References(r => r.Login, "UserId").Not.Nullable();
}
}
}
This is my Repository:
using System;
using System.Linq;
using NHibernate;
using FluentNHibernate.Cfg;
using FluentNHibernate.Cfg.Db;
using NHibernate.Linq;
using MyData.Data.Mappings;
namespace MyData.Data.Repository
{
public class LoginRepository
{
private readonly ISessionFactory _sessionFactory;
ISession _session;
public LoginRepository()
{
_sessionFactory = this.CreateSessionFactory();
_session = _sessionFactory.OpenSession();
}
private ISessionFactory CreateSessionFactory()
{
string connString = "MyDataConnectionString";
FluentConfiguration config = Fluently.Configure()
.Database(MsSqlConfiguration.MsSql2005.ConnectionString(
x => x.FromConnectionStringWithKey(connString)))
.ExposeConfiguration(
c => c.SetProperty("current_session_context_class", "webContext"))
.Mappings(m => m.FluentMappings.AddFromAssemblyOf<Login>());
return config.BuildSessionFactory();
}
public IQueryable<Login> GetAllLogins()
{
return _session.Linq<Login>();
}
}
}
Whenever it gets to config.BuildSessionFactory() it throws the following error:
An invalid or incomplete configuration was used while creating a SessionFactory.
The inner Exception is:
Exception has been thrown by the target of an invocation.
Is this the correct way to approach this? Any ideas or tweaks would be greatly appreciated!
Sean, you cannot use GeneratedBy.Guid() on LoginId because it's an int. Try to use another generator or change the type of LoginId.
I suspect you cannot use "private set" on UserId because it's the Id.
Also, HasOne(d => d.User).ForeignKey("UserId") means that if you expose your database model from your mappings, the name of the FK constraint will be "UserId". It seems to me that your intention was to specify the name column that holds the PK on table "Users".
These links have more infos:
http://wiki.fluentnhibernate.org/Fluent_mapping
http://jagregory.com/writings/i-think-you-mean-a-many-to-one-sir/
one-to-one fluent nhibernate?
It seems you forgot to specify proxy factory. It can be done like this:
OracleDataClientConfiguration persistenceConfigurer = OracleDataClientConfiguration
.Oracle10
.ConnectionString(connectionStringBuilder => connectionStringBuilder.FromAppSetting("Main.ConnectionString"))
.ProxyFactoryFactory<ProxyFactoryFactory>()
.CurrentSessionContext<ThreadStaticSessionContext>()
.AdoNetBatchSize(25)
.DoNot.ShowSql();
FluentConfiguration cfg = Fluently.Configure()
.Database(persistenceConfigurer)
.Mappings(m => m.FluentMappings.AddFromAssemblyOf<UserMap>());
return cfg.BuildConfiguration();
It appears that NHibernate cannot automap more than one IList of a given type in an entity.
Consider the following two entities (based on the Examples.FirstProject sample code that is included with the Fluent NHibernate source code).
public class Employee
{
public virtual int Id { get; private set; }
public virtual string FirstName { get; set; }
public virtual string LastName { get; set; }
}
public class Store
{
public virtual int Id { get; private set; }
public virtual IList<Employee> Staff { get; set; }
public virtual IList<Employee> Managers { get; set; }
}
This seems to be a perfectly valid object model - each store has several staff employees and several manager employees.
But when I automap, the Staff and Managers lists are stored in the Employee table,all with the same foreign key.
Employee Table
Id FirstName LastName Store_id
3 Daisy Harrison 1
4 Jack Torrance 1
5 Sue Walkters 1
6 Tom Tommorow 1
7 Dick Diggler 1
The net result is that when the data is read back out of the database, both Staff and Managers lists are populated with every row in the table.
This looks like a bug in Automapping to me, but I'm fairly new to NHibernate in any form, and don't fully know it's limitations yet.
In any case, how can I make NHibernate treat the two lists as distinct?
If possible, I'd appreciate an Automapping code fragment that directly addresses the sample code I've provided (e.g. something like "put this exact override in the .Mappings section of your CreateSessionFactory").
This is because I'm only somewhat familiar with Automapping, and not at all familiar with the older ways of doing things, which means I can't "fill in the blanks" very well yet.
But if you only have time to point me in the right direction, that would be helpful too.
Here's my CreateSessionFactory code, to give some context:
private static ISessionFactory CreateSessionFactory()
{
ISessionFactory sessionFactory = null;
const string autoMapExportDir = "AutoMapExport";
if( !Directory.Exists(autoMapExportDir) )
Directory.CreateDirectory(autoMapExportDir);
try
{
var autoPersistenceModel =
AutoMap.AssemblyOf<Product>()
.Where(t => t.Namespace == "Examples.FirstProject.Entities")
.Conventions.Add( DefaultCascade.All() )
;
sessionFactory = Fluently.Configure()
.Database(SQLiteConfiguration.Standard
.UsingFile(DbFile)
.ShowSql()
)
.Mappings(m => m.AutoMappings.Add(autoPersistenceModel)
.ExportTo(autoMapExportDir)
)
.ExposeConfiguration(BuildSchema)
.BuildSessionFactory()
;
}
catch (Exception e)
{
Console.WriteLine(e);
}
return sessionFactory;
}
Paul Batum answered my question here, and provided a standalone working example here (click the Download button after you navigate to the linked page).
The following code is copied from his answer. The key point is in the StoreMap class at the end of the listing, which sets up an override with a Where clause that uses the IsManager property in Employee.
Note that (at least with v. 1.0.0.594) there is one big gotcha with Automapping - the mapping class (e.g. StoreMap) cannot be in the same Namespace as the domain class (e.g. Store)!
Otherwise, NHibernate will throw "NHibernate.MappingException:
(XmlDocument)(2,4): XML validation error: ..." , with
absolutely no indication of what or where the real problem is.
This is probably a bug that may be fixed in later versions of Fluent NHibernate.
public class Employee
{
public virtual int Id { get; private set; }
public virtual string FirstName { get; set; }
public virtual string LastName { get; set; }
public virtual bool IsManager { get; set; }
}
public class Store
{
public virtual int Id { get; private set; }
public virtual IList<Employee> Staff { get; private set; }
public virtual IList<Employee> Managers { get; private set; }
public Store()
{
Staff = new List<Employee>();
Managers = new List<Employee>();
}
public void AddManager(Employee employee)
{
employee.IsManager = true;
this.Managers.Add(employee);
}
public void AddStaff(Employee employee)
{
this.Staff.Add(employee);
}
}
Here is the mapping override for store:
// Must be in different Namespace from class Store!!!
public class StoreMap : IAutoMappingOverride<Store>
{
public void Override(AutoMapping<Store> mapping)
{
mapping.HasMany(x => x.Managers)
.Cascade.All()
.Where("(IsManager = 1)");
mapping.HasMany(x => x.Staff)
.Cascade.All()
.Where("(IsManager = 0)");
}
}