1

我想简单地选择数据库中的版本和模板组合。

这是数据库结构:

CREATE Table ProductVersion(
id int not null primary key Identity,
Product_ID int not null,
Version decimal(18,1) not null,
Active bit not null,
LicenseVersion int not null,
FOREIGN KEY (Product_ID) References Products(id)
)

Create Table CodeTemplates(
id int not null primary key Identity,
Name varchar(50) not null,
DLT_ID int not null,
QuantityRule_ID int,
SerialRule_ID int,
DurationRule_ID int,
foreign key (DLT_ID) References DisplayedLicenseTypes(id),
foreign key (QuantityRule_ID) References Rulesets(id),
foreign key (SerialRule_ID) References Rulesets(id),
foreign key (DurationRule_ID) References Rulesets(id)
)

CREATE table ProductVersion_CodeTemplate(
ProductVersion_ID int not null,
CodeTemplate_ID int not null,
CONSTRAINT PK_Restrictions Primary Key Clustered
(
    ProductVersion_ID asc,
    CodeTemplate_ID asc
),
foreign key (ProductVersion_ID) References ProductVersion(id),
foreign key (CodeTemplate_ID) References CodeTemplates(id)
)

以下是用于 productversion 和 CodeTemplates 的当前模型:

[Table("ProductVersion")]
public class ProductVersionModel
{
    public int ID { get; set; }
    public virtual ProductModel Product { get; set; }
    [DisplayFormat(DataFormatString = "{0:F1}",ApplyFormatInEditMode=true)]
    public decimal Version { get; set; }
    public bool Active { get; set; }
    public int LicenseVersion { get; set; }
}
[Table("CodeTemplates")]
public class TemplateModel
{
    public int ID { get; set; }

    public string Name { get; set; }
    public virtual DisplayedLicenseTypeModel DLT { get; set; }
    public virtual List<LicenseFlagModel> LicenseFlags { get; set; }
    public virtual RulesetModel QuantityRule { get; set; }
    public virtual RulesetModel SerialRule { get; set; }
    public virtual RulesetModel DurationRule { get; set; }
}

这是我尝试使用的:

[Table("ProductVersion_CodeTemplate")]
public class ProductVersionWithTemplateModel
{
    [Key, Column(Order = 0)]
    public virtual ProductVersionModel ProductVersion { get; set; }
    [Key, Column(Order = 1)]
    public virtual TemplateModel CodeTemplate { get; set; }
}

弹出的第一个错误是:

在模型生成期间检测到一个或多个验证错误:

System.Data.Edm.EdmEntityType: : EntityType 'ProductVersionWithTemplateModel' 没有定义键。定义此 EntityType 的键。System.Data.Edm.EdmEntitySet: EntityType: EntitySet “AppliedTemplates” 基于没有定义键的类型“ProductVersionWithTemplateModel”。

我在这里缺少什么,一个 dbmodelbuilder 绑定?

回答(因为评论中的格式很糟糕):

[Table("ProductVersion_CodeTemplate")]
    public class ProductVersionWithTemplateModel
    {
        [Key, Column(Order = 0)]
        public int ProductVersion_ID { get; set; }
        [ForeignKey("ProductVersion_ID")]
        public virtual ProductVersionModel ProductVersion { get; set; }
        [Key, Column(Order = 1)]
        public int CodeTemplate_ID { get; set; }
        [ForeignKey("CodeTemplate_ID")]
        public virtual TemplateModel CodeTemplate { get; set; }
    }
4

1 回答 1

0

也许您应该包括这样的参考:

[Table("ProductVersion_CodeTemplate")]
public class ProductVersionWithTemplateModel
{
 [Key, Column(Order = 0)]
 public int ProductVersionModelId { get; set; }
 [ForeignKey("ProductVersionModelId")]
 public virtual ProductVersionModel ProductVersion { get; set; }
 [Key, Column(Order = 1)]
 public int TemplateModelId { get; set; }
 [ForeignKey("TemplateModelId")]
 public virtual TemplateModel CodeTemplate { get; set; }
}
于 2012-09-04T19:56:27.783 回答