Created
July 18, 2011 17:17
-
-
Save codeprogression/1090085 to your computer and use it in GitHub Desktop.
SO6727128
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Data; | |
using FluentNHibernate.Cfg; | |
using FluentNHibernate.Cfg.Db; | |
using FluentNHibernate.Mapping; | |
using NHibernate; | |
using NHibernate.Tool.hbm2ddl; | |
namespace SO6727128 | |
{ | |
public class Post | |
{ | |
public Post() | |
{ | |
Tags = new List<Tag>(); | |
} | |
public virtual Guid Id { get; set; } | |
public virtual string Title { get; set; } | |
public virtual IList<Tag> Tags { get; set; } | |
public virtual void AddTag(Tag tag) | |
{ | |
tag.Posts.Add(this); | |
Tags.Add(tag); | |
} | |
} | |
public class Tag | |
{ | |
public Tag() | |
{ | |
Posts = new List<Post>(); | |
} | |
public virtual Guid Id { get; set; } | |
public virtual string Name { get; set; } | |
public virtual IList<Post> Posts { get; set; } | |
public virtual void AddPost(Post post) | |
{ | |
post.Tags.Add(this); | |
Posts.Add(post); | |
} | |
} | |
public class PostMap : ClassMap<Post> | |
{ | |
public PostMap() | |
{ | |
Table("Post"); | |
Id(x => x.Id).GeneratedBy.GuidComb(); | |
Map(x => x.Title); | |
HasManyToMany(x => x.Tags).Table("PostTags").Cascade.SaveUpdate().Inverse(); | |
} | |
} | |
public class PostTagMap : ClassMap<Tag> | |
{ | |
public PostTagMap() | |
{ | |
Table("Tag"); | |
Id(x => x.Id).GeneratedBy.GuidComb(); | |
Map(x => x.Name); | |
HasManyToMany(x => x.Posts).Table("PostTags").Cascade.None(); | |
} | |
} | |
public class NHibernateConfig | |
{ | |
public void TestMapping() | |
{ | |
var sessionFactory = CreateSessionFactory(); | |
var session = sessionFactory.OpenSession(); | |
//Insert Records | |
session.BeginTransaction(); | |
var tag = new Tag {Name = "Tag1"}; | |
var post1 = new Post {Title = "Post 1"}; | |
var post2 = new Post { Title = "Post 2" }; | |
post1.AddTag(tag); | |
post2.AddTag(tag); | |
session.Save(post1); | |
session.Save(post2); | |
session.Transaction.Commit(); | |
//Assert | |
session.BeginTransaction(); | |
System.Diagnostics.Debug.Assert(post1.Tags[0] == tag); | |
System.Diagnostics.Debug.Assert(post2.Tags[0] == tag); | |
System.Diagnostics.Debug.Assert(tag.Posts[0]==post1); | |
System.Diagnostics.Debug.Assert(tag.Posts[1] == post2); | |
session.Transaction.Commit(); | |
} | |
public ISessionFactory CreateSessionFactory() | |
{ | |
return CreateSessionFactory(GetSqlConfiguration(), BuildSchema); | |
} | |
protected virtual Func<IPersistenceConfigurer> GetSqlConfiguration() | |
{ | |
var msSql2005 = MsSqlConfiguration.MsSql2008; | |
Action<MsSqlConnectionStringBuilder> expression = | |
c => c.Server("(local)").Database("StackOverflow").TrustedConnection(); | |
return () => msSql2005.ConnectionString(expression) | |
.UseReflectionOptimizer() | |
.IsolationLevel(IsolationLevel.ReadUncommitted) | |
.ShowSql(); | |
} | |
public virtual ISessionFactory CreateSessionFactory(Func<IPersistenceConfigurer> configuration, | |
Action<NHibernate.Cfg.Configuration> schema) | |
{ | |
try | |
{ | |
var configure = Fluently.Configure(); | |
var database = configure.Database(configuration); | |
var mappings = database.Mappings(CreateMappings()); | |
var fluentConfiguration = mappings.ExposeConfiguration(schema); | |
return fluentConfiguration.BuildSessionFactory(); | |
} | |
catch (Exception ex) | |
{ | |
throw; | |
} | |
} | |
private Action<MappingConfiguration> CreateMappings() | |
{ | |
return m => m.FluentMappings.Add(typeof (PostMap)).Add(typeof (PostTagMap)); | |
} | |
protected virtual void BuildSchema(NHibernate.Cfg.Configuration config) | |
{ | |
new SchemaExport(config).Create(true, true); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
------ Test started: Assembly: FNHSpike.dll ------ | |
if exists (select 1 from sys.objects where object_id = OBJECT_ID(N'[FKCDC8255AD35EB19B]') AND parent_object_id = OBJECT_ID('PostTags')) | |
alter table PostTags drop constraint FKCDC8255AD35EB19B | |
if exists (select 1 from sys.objects where object_id = OBJECT_ID(N'[FKCDC8255AC6B70A23]') AND parent_object_id = OBJECT_ID('PostTags')) | |
alter table PostTags drop constraint FKCDC8255AC6B70A23 | |
if exists (select * from dbo.sysobjects where id = object_id(N'Post') and OBJECTPROPERTY(id, N'IsUserTable') = 1) drop table Post | |
if exists (select * from dbo.sysobjects where id = object_id(N'PostTags') and OBJECTPROPERTY(id, N'IsUserTable') = 1) drop table PostTags | |
if exists (select * from dbo.sysobjects where id = object_id(N'Tag') and OBJECTPROPERTY(id, N'IsUserTable') = 1) drop table Tag | |
create table Post ( | |
Id UNIQUEIDENTIFIER not null, | |
Title NVARCHAR(255) null, | |
primary key (Id) | |
) | |
create table PostTags ( | |
Post_id UNIQUEIDENTIFIER not null, | |
Tag_id UNIQUEIDENTIFIER not null | |
) | |
create table Tag ( | |
Id UNIQUEIDENTIFIER not null, | |
Name NVARCHAR(255) null, | |
primary key (Id) | |
) | |
alter table PostTags | |
add constraint FKCDC8255AD35EB19B | |
foreign key (Tag_id) | |
references Tag | |
alter table PostTags | |
add constraint FKCDC8255AC6B70A23 | |
foreign key (Post_id) | |
references Post | |
NHibernate: INSERT INTO Post (Title, Id) VALUES (@p0, @p1);@p0 = 'Post 1' [Type: String (4000)], @p1 = f2c61d51-12b4-4583-b88d-9f2400bc0b57 [Type: Guid (0)] | |
NHibernate: INSERT INTO Tag (Name, Id) VALUES (@p0, @p1);@p0 = 'Tag1' [Type: String (4000)], @p1 = b754b508-0533-4f9f-8572-9f2400bc0b5f [Type: Guid (0)] | |
NHibernate: INSERT INTO Post (Title, Id) VALUES (@p0, @p1);@p0 = 'Post 2' [Type: String (4000)], @p1 = 6d5388d2-55bd-490a-a27b-9f2400bc0b5f [Type: Guid (0)] | |
NHibernate: INSERT INTO PostTags (Tag_id, Post_id) VALUES (@p0, @p1);@p0 = b754b508-0533-4f9f-8572-9f2400bc0b5f [Type: Guid (0)], @p1 = f2c61d51-12b4-4583-b88d-9f2400bc0b57 [Type: Guid (0)] | |
NHibernate: INSERT INTO PostTags (Tag_id, Post_id) VALUES (@p0, @p1);@p0 = b754b508-0533-4f9f-8572-9f2400bc0b5f [Type: Guid (0)], @p1 = 6d5388d2-55bd-490a-a27b-9f2400bc0b5f [Type: Guid (0)] | |
1 passed, 0 failed, 0 skipped, took 1.33 seconds (Ad hoc). |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment