SubSonic主键问题

发布于 2024-11-04 13:50:38 字数 4069 浏览 0 评论 0原文

在我们的组织中,我们必须使用名称 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) 我遇到了问题,但我似乎不知道如何解决它,我想我应该去 subsonic 的网站并找到两秒钟内就有答案,当然这不会发生。

感谢您的任何输入...

我正在使用版本 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 - 嗯?

At our organization we have to use the name ID_NO for the primary keys in our tables, a little strange to me but I can't change that however it is causing subsonic to complain and I am hoping someone with more experience can help me out.

Here is the error I get

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

Here is my query(s) there is actually two.

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>();

it wasn't until I added && !excludedVendors.Any(x => x == vndMapping.VENDOR_ID) that I got the problem, I can't seem to figure out how to fix it though, i thought I would go to subsonic's site and find the answer in two seconds of course that isn't happening.

Thanks for any input...

I am using version 3.0.0.3, I just found this in the structs file which we are generating via a 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?
            });

Notice the IsPrimaryKey = true - Hmm?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

北斗星光 2024-11-11 13:50:38

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

因此,您不能像 Drackir 所示那样尝试手动将其放在那里。下次生成模板时,更改将会消失,并且您无法在分部类中应用该属性,因为该字段已经定义。

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

您可以通过多种方法更改模板来实现此目的。最简单的方法可能是告诉模板完全忽略 ID_NO 字段,然后在部分类中手动定义它们。另一种选择是让模板在生成属性时将属性添加到适当的字段。

事实上,每次我回答 SubSonic 的问题时——而且随着时间的推移——我一直在想,“我应该告诉他们在可以的时候远离 SubSonic。”它有很多这样的陷阱,而且它确实不再被开发到任何实质性程度。我可能会因为这样说而受到一些讨厌,但我知道我们几个月前就决定在所有项目上尽可能远离它。

One problem is that you have to apply the SubSonicPrimaryKey attribute to the property for the primary key in your model object, which is generated code.

So you can't just try and put it there manually like Drackir is showing. Next time your templates get generated that change will get blown away, and you can't apply the attribute in a partial class because the field is already defined.

You will have to change the templates (also, perhaps you should update to the most current version of everything, the commented out code from your structs.cs file shows you have some things mismatched and out of date).

There are several ways you could change the template to make this work. The easiest might be to tell the template to ignore ID_NO fields completely and then manually define them all in partial classes. Another option would be to make the template add the attribute to the appropriate field when it generates them.

Really, though, every time I answer a SubSonic question - and moreso as time goes on - I keep thinking, "I should tell them to get the heck away from SubSonic while they can." It's got plenty of pitfalls like this and it really isn't being developed to any substantial degree any longer. I'll probably get some hate for saying that, but I know we decided months ago to get as far away from it as possible on all projects.

离线来电— 2024-11-11 13:50:38

据我所知,SubSonic 有助于链接您的类(也许会生成它们?)和您的数据库。根据公约文档

主键

如果您想使用SubSonic来访问
你的表,你需要有一个 Primary
为您的表定义的键。这是
在每种情况下都有良好的做法,我们
需要它与您一起做某些事情
桌子。如果您没有主键
定义,你的班级不会
生成。

因此,假设您已经在表结构上定义了主键,但它仍然不起作用,我们需要找到其他东西来告诉它要查找什么。该页面还接着说:

主键

如果您将列称为“ID”或“Key”或
“[ClassName]ID” – 无论它是什么类型
– 这将是您的主键。如果
你还有其他事情可以考虑
使用主键属性
[SubSonicPrimaryKey] 包含在
SubSonic.SqlGeneration.Schema
命名空间,我们将使用该列。

这里的关键是,您必须通过添加 [SubSonicPrimaryKey] 属性来显式告诉它使用哪个列(类属性/字段)作为主键。为此,请进入您的类文件(我猜无论 vndMapping 是什么类型)并在 VENDOR_ID 属性声明上方添加 [SubSonicPrimaryKey]

例如,类似这样的内容:

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);

From what I gather SubSonic helps link up your classes (perhaps generates them?) and your database. According to the Convention docs:

Primary Keys

If you want to use SubSonic to access
your table, you need to have a Primary
Key defined for your table. This is
good practice in every case and we
need it to do certain things with your
table. If you don't have a Primary Key
defined, your class won't be
generated.

So, assuming you've defined a Primary Key on your table structure and it's still not working, we need to find something else to tell it what to look for. The page also goes on to say this:

Primary Keys

If you call a column “ID” or “Key” or
“[ClassName]ID” – no matter it’s type
– that will be your Primary Key. If
you have other things in mind you can
use a primary key attribute
[SubSonicPrimaryKey] contained in the
SubSonic.SqlGeneration.Schema
namespace and we’ll use that column.

The key here is that you have to explicitly tell it what column (class property/field) to use as the primary key by adding the [SubSonicPrimaryKey] attribute. To do this, go into your class file (I guess for whatever type vndMapping is) and add [SubSonicPrimaryKey] above the VENDOR_ID property's declaration.

So for example, something like this:

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

Please note I don't use SubSonic but from what their site says, this seems to make sense.

Edit
Based on your edit, try replacing Columns.Add(new DatabaseColumn("ID_NO", this) { /*...*/ }); with something like 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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文