fluent-nhibernate: not getting the records
I have an entity like
public class SKU
{
//public int Id { get; set; }
public string FactoruCode { get; set; }
public string Ptoduct { get; set; }
}
and mapping defined as
public class SKUMap : ClassMap<SKU>
{
publi开发者_JAVA百科c SKUMap()
{
Table("MST_PRODUCT");
Not.LazyLoad();
Id(x => x.Ptoduct).GeneratedBy.Assigned();
Map(x => x.Ptoduct, "PRODUCT_NAME");
Map(x => x.FactoruCode, "FACTORY_CODE");
}
}
and retrieving the records like
class Program
{
static void Main()
{
var sessionFactory = CreateSessionFactory();
using (var session = sessionFactory.OpenSession())
{
using (session.BeginTransaction())
{
var skus = session.CreateCriteria(typeof(SKU)).List<SKU>();
foreach (var sku in skus)
{
Console.WriteLine(sku.Ptoduct);
}
}
}
}
private static ISessionFactory CreateSessionFactory()
{
var cfg = OracleClientConfiguration.Oracle10
.ConnectionString(c =>
c.Is(
@"DATA SOURCE=SERVER_NAME;PERSIST SECURITYINFO=True;USER ID=USER_ID;Password=PWD"));
return Fluently.Configure()
.Database(cfg).Mappings(m => m.FluentMappings.AddFromAssemblyOf<Program>())
.ExposeConfiguration(BuildSchema).BuildSessionFactory();
}
private static void BuildSchema(NHibernate.Cfg.Configuration config)
{
new SchemaExport(config).Create(false, true);
}
}
but the table has more columns than specified for entity. This code executes well, but I'm not able to get the list of SKUs (table has more than 8000 rows).
Please help me to understand the problem.
Your SKU map is wrong. Why have you defined PRODUCT_NAME
as an Id column? You need to fix it by setting the Id to an Id column (which you have commented out):
Id(x => x.Id, "NAME_OF_YOUR_ID_COLUMN_HERE").GeneratedBy.Assigned();
Map(x => x.Ptoduct, "PRODUCT_NAME");
If PRODUCT_NAME
is indeed the Id, you need to set it like this:
Id(x => x.Ptoduct, "PRODUCT_NAME").GeneratedBy.Assigned();
and remove the other line:
Map(x => x.Ptoduct, "PRODUCT_NAME");
Also, if your database has more fields or tables then you are mapping, it can give you many errors. To resolve them, you need to set use_proxy_validator
to false
in your configuration.
EDIT:
NHibernate requires an Id column to work properly. I don't even know that if it does work without actually having a column declared as an Id column. Even if you declare Ptoduct
as an Id column, you will not be able to properly query the database as querying for any of all objects with the same Ptoduct
will return the topmost object.
精彩评论