SubSonic主要关键问题

时间:2011-04-28 17:37:22

标签: c# subsonic

在我们的组织中,我们必须使用名称ID_NO作为我们表格中的主键,对我来说有点奇怪,但我不能改变它,但它会导致亚音速投诉,我希望有更多经验的人可以帮助我出去

这是我得到的错误

Can't decide which property to consider the Key - you can create one called 'ID' or mark one with SubSonicPrimaryKey attribute [System.InvalidOperationException]

这是我的查询实际上有两个。

var excludedVendors = (from vndMapping in db.COMPANIES_VND_MAPPINGS
                         join compView in db.COMPANIES_COMPANIES_VS on vndMapping.VENDOR_ID equals compView.COMPANY_ID
                         join cmpTier in db.CT_CT_CMP_TIERS on compView.TIER_CODE equals cmpTier.CODE
                         where (vndMapping.OEM_ID == null || vndMapping.OEM_ID == oemId)
                         && (vndMapping.MODEL_ID == null || vndMapping.MODEL_ID == modelId)
                         && (vndMapping.MODALITY_ID == null || vndMapping.MODALITY_ID == modalityId)
                         && (vndMapping.CLASS_ID == null || vndMapping.CLASS_ID == productTypeId)
                         && vndMapping.EXCLUDE == null
                         select
                           vndMapping.VENDOR_ID
                                  );

  possibleVendors = (from vndMapping in db.COMPANIES_VND_MAPPINGS
                     join compView in db.COMPANIES_COMPANIES_VS on vndMapping.VENDOR_ID equals compView.COMPANY_ID
                     join cmpTier in db.CT_CT_CMP_TIERS on compView.TIER_CODE equals cmpTier.CODE
                     where (vndMapping.OEM_ID == null || vndMapping.OEM_ID == oemId)
                     && (vndMapping.MODEL_ID == null || vndMapping.MODEL_ID == modelId)
                     && (vndMapping.MODALITY_ID == null || vndMapping.MODALITY_ID == modalityId)
                     && (vndMapping.CLASS_ID == null || vndMapping.CLASS_ID == productTypeId)
                     && !excludedVendors.Any(x => x == vndMapping.VENDOR_ID)
                     && (compView.COMPANY_TYPE_ID == 2 || compView.COMPANY_TYPE_ID == 3)
           select new VendorProxy
           {
             DiscountPercent = (double)compView.DISCOUNT_PERCENT,
             OrderNo = cmpTier.ORDER_NO,
             PhoneNumber = "",
             TierCode = cmpTier.CODE,
             TierDescription = cmpTier.DESCRIPTION,
             VendorId = vndMapping.VENDOR_ID,
             VendorName = compView.COMPANY_NAME
           }).OrderBy(x => x.OrderNo).ThenBy(x => x.VendorName).ToList<VendorProxy>();

直到我添加&& !excludedVendors.Any(x => x == vndMapping.VENDOR_ID)我才得到问题,我似乎无法弄清楚如何修复它,我想我会去亚音速网站并在两秒内找到答案当然这没有发生。

感谢您的任何意见......

我使用的是3.0.0.3版本,我刚刚在我们通过t4生成的结构文件中找到了这个

  public class COMPANIES_VND_MAPPINGTable: DatabaseTable {

        public COMPANIES_VND_MAPPINGTable(IDataProvider provider):base("VND_MAPPINGS",provider){
            ClassName = "COMPANIES_VND_MAPPING";
            SchemaName = "COMPANIES";


            Columns.Add(new DatabaseColumn("ID_NO", this)
            {
                IsPrimaryKey = true,
                DataType = DbType.Decimal,
                IsNullable = false,
                AutoIncrement = false,
                IsForeignKey = false,
                MaxLength = 15
                //CleanName = "ID_NO" Temporarily removed. The 'CleanName' property does not exist on this class in the SubSonic3 master repo on GitHub. Did someone miss a file checkin?
            });

            Columns.Add(new DatabaseColumn("VENDOR_ID", this)
            {
                IsPrimaryKey = false,
                DataType = DbType.Decimal,
                IsNullable = false,
                AutoIncrement = false,
                IsForeignKey = false,
                MaxLength = 15
                //CleanName = "VENDOR_ID" Temporarily removed. The 'CleanName' property does not exist on this class in the SubSonic3 master repo on GitHub. Did someone miss a file checkin?
            });

注意IsPrimaryKey = true - 嗯?

2 个答案:

答案 0 :(得分:2)

一个问题是您必须将SubSonicPrimaryKey属性应用于模型对象中主键的属性,即生成的代码。

所以你不能像Drackir那样手动把它放在那里。下次生成模板时,更改将被吹走,并且您无法在分部类中应用该属性,因为该字段已经定义。

你必须更改模板(也许你应该更新到最新版本的所有内容,你的structs.cs文件中注释掉的代码显示你有一些不匹配和过时的东西)。

有几种方法可以更改模板以使其工作。最简单的方法是告诉模板完全忽略ID_NO个字段,然后在部分类中手动定义它们。另一种选择是让模板在生成属性时将属性添加到适当的字段。

但是,真的,每次我回答SubSonic的问题 - 而且随着时间的推移 - 我一直在想,&#34;我应该告诉他们尽可能远离SubSonic。&#34;它有很多像这样的陷阱,它实际上不再发展到任何实质性的程度。我可能会因此而讨厌,但我知道我们几个月前决定在所有项目中尽可能远离它。

答案 1 :(得分:0)

从我收集的内容中,SubSonic可以帮助您链接课程(可能会生成它们?)和您的数据库。根据{{​​3}}:

  

主键

     

如果您想使用SubSonic进行访问   你的桌子,你需要一个小学   为您的表定义的键。这是   在每个案例和我们的良好实践   需要它与你做某些事情   表。如果您没有主键   定义,你的班级不会   生成。

因此,假设您已经在表结构上定义了主键并且它仍然无效,我们需要找到其他内容来告诉它要查找的内容。该页面还继续说:

  

主键

     

如果您拨打“ID”或“密钥”列或   “[ClassName] ID” - 无论其类型如何    - 那将是你的主键。如果   你可以考虑其他事情   使用主键属性   [SubSonicPrimaryKey]包含在   SubSonic.SqlGeneration.Schema   命名空间,我们将使用该列。

这里的关键是你必须通过添加[SubSonicPrimaryKey]属性明确告诉它使用什么列(类属性/字段)作为主键。要做到这一点,请进入您的类文件(我想无论vndMapping是什么类型)并在[SubSonicPrimaryKey]属性声明上方添加VENDOR_ID

例如,像这样:

class VendorMap {
    [SubSonicPrimaryKey]
    public int VENDOR_ID { get; set; }
}

请注意我不使用SubSonic,但从他们的网站上说,这似乎是有意义的。

修改
根据您的修改,尝试使用以下内容替换Columns.Add(new DatabaseColumn("ID_NO", this) { /*...*/ });

[SubSonicPrimaryKey]
DatabaseColumn idcol = new DatabaseColumn("ID_NO", this)
{
    IsPrimaryKey = true,
    DataType = DbType.Decimal,
    IsNullable = false,
    AutoIncrement = false,
    IsForeignKey = false,
    MaxLength = 15
    //CleanName = "ID_NO" Temporarily removed. The 'CleanName' property does not exist on this class in the SubSonic3 master repo on GitHub. Did someone miss a file checkin?
};
Columns.Add(idcol);