C# 在C中使用NHibernate为值对象配置复杂类型#
我不熟悉DDD和NHibernate,当我尝试使用NHibernate fluent为表配置时(首先使用代码) 在EntityFramework中,我们可以使用ComplexTypeConfiguration来配置值对象,并在许多表中使用该配置,但我不知道如何为EntityFramework这样的值对象单独配置C# 在C中使用NHibernate为值对象配置复杂类型#,c#,.net,fluent-nhibernate,domain-driven-design,C#,.net,Fluent Nhibernate,Domain Driven Design,我不熟悉DDD和NHibernate,当我尝试使用NHibernate fluent为表配置时(首先使用代码) 在EntityFramework中,我们可以使用ComplexTypeConfiguration来配置值对象,并在许多表中使用该配置,但我不知道如何为EntityFramework这样的值对象单独配置 internal class EmployeeConfiguration : ClassMap<Employee> { public EmployeeConfigur
internal class EmployeeConfiguration : ClassMap<Employee>
{
public EmployeeConfiguration()
{
Table("Employees");
Id(emp => emp.Id).GeneratedBy.Identity();
Component(emp => emp.FullName, name =>
{
name.Map(ele => ele.FirstName).Column("FirstName").Length(255).Not.Nullable();
name.Map(ele => ele.LastName).Column("LastName").Length(255).Not.Nullable();
name.Map(ele => ele.MiddleName).Column("MiddleName").Length(255).Nullable();
});
}
}
内部类EmployeeConfiguration:ClassMap
{
公共雇员配置()
{
表(“雇员”);
Id(emp=>emp.Id).GeneratedBy.Identity();
组件(emp=>emp.FullName,name=>
{
name.Map(ele=>ele.FirstName).Column(“FirstName”).Length(255).Not.Nullable();
name.Map(ele=>ele.LastName).Column(“LastName”).Length(255).Not.Nullable();
name.Map(ele=>ele.MiddleName).Column(“MiddleName”).Length(255.Nullable();
});
}
}
我在谷歌上搜索解决方案,但没有什么真正有用的
非常感谢您的帮助。据我所知,您需要一个可在各种表上实现的可重用ComplexType配置。据报道,到目前为止一切顺利 “如果要映射一个实体,就要使用ClassMap;如果要映射一个值对象,就要使用ComponentMap类”。因此,我基于它构建了一个解决方案,也许这不是正确的答案,但希望这会有所帮助
//Complex type class
public class Address
{
public virtual string City { get; set; }
public virtual string Street { get; set; }
public virtual string StateOrProvince { get; set; }
public virtual string Country { get; set; }
}
//Entity
public class Employee
{
public virtual int Id { get; set; }
public virtual string FullName { get; set; }
public virtual Address Address { get; set; }
public virtual decimal Salary { get; set; }
}
//ComplexTypeConfiguration
public class AddressMap : ComponentMap<Address>
{
public AddressMap()
{
Map(c => c.City).Column("City").Length(255).Not.Nullable();
Map(c => c.Country).Column("Country").Length(255);
Map(c => c.StateOrProvince).Nullable();
Map(c => c.Street).Nullable();
}
}
//Mapping
public class EmployeeMap : ClassMap<Employee>
{
public EmployeeMap()
{
Table("Employees");
Id(c => c.Id);
Map(c => c.FullName);
Map(c => c.Salary);
Component(c => c.Address);
}
}
//Connection string
public class NHibernateHelper
{
public static ISession OpenSession()
{
ISessionFactory sessionFactory = Fluently.Configure()
.Database(MsSqlConfiguration.MsSql2012
.ConnectionString(@"Server=.;Database=TestDB;Trusted_Connection=True;")
.ShowSql())
.Mappings(m => m.FluentMappings.AddFromAssemblyOf<EmployeeMap>())
.ExposeConfiguration(cfg => new SchemaUpdate(cfg).Execute(false, true))
.BuildSessionFactory();
Console.WriteLine("Database Connected");
return sessionFactory.OpenSession();
}
}
//Test script
static void Main(string[] args)
{
using (ISession session = NHibernateHelper.OpenSession())
{
Employee employee = new Employee()
{
Id = 1,
FullName = "Nidust Ashen",
Salary = 12345678,
Address = new Address()
{
City = "test1",
Street = "test2",
Country = "test3",
StateOrProvince = "test4",
}
};
session.Save(employee);
Console.WriteLine("Done!");
Console.ReadLine();
}
}
//复杂类型类
公共课堂演讲
{
公共虚拟字符串City{get;set;}
公共虚拟字符串Street{get;set;}
公共虚拟字符串状态提供{get;set;}
公共虚拟字符串国家{get;set;}
}
//实体
公营雇员
{
公共虚拟整数Id{get;set;}
公共虚拟字符串全名{get;set;}
公共虚拟地址{get;set;}
公共虚拟十进制工资{get;set;}
}
//ComplexTypeConfiguration
公共类AddressMap:ComponentMap
您试过这个吗?谢谢你的帮助,但这不是我的问题^^