2

我在这里搜索了很多问题,听起来与我的相似,但如果它与我的问题相匹配,则没有一个,所以我希望对这张票有所帮助..

我使用 EF Code First 并尝试将其映射到现有(旧版)MySQL 数据库。在我的属性上工作正常,除了一个,我不知道为什么..

为空的模型:

[Table("einheitenstamm")]
public class Unit : ClassicEntity
{
    [Key]
    [Column("estID")]
    public int Id { get; set; }

    [Column("estEinheit")]
    public string Name { get; set; }
}

包含对 Unit 的引用的模型:

[Table("artikeldaten_preise")]
public class ArticlePrice : ClassicEntity
{
    [Key]
    [Column("id")]
    public int Id { get; set; }

    [Column("einheit")]
    [ForeignKey("Id")]
    public virtual Unit Unit { get; set; } /* is always null!!!!! */

    [Column("preisliste")]
    [ForeignKey("Id")]
    public virtual Pricelist Pricelist { get; set; } /* gets loaded without problems */

    [Column("artikel")]
    [ForeignKey("Id")]
    public virtual Article Article { get; set; } /* gets loaded without problems */


    [Column("preis")]
    public double Price { get; set; }
}

数据库表创建:

CREATE TABLE `artikeldaten_preise` (
    `id` INT(11) NOT NULL AUTO_INCREMENT,
    `preisliste` INT(11) NOT NULL,
    `artikel` VARCHAR(10) NOT NULL,
    `preis` DECIMAL(10,2) NOT NULL,
    `einheit` INT(11) NOT NULL,
    `changed` DATETIME NULL DEFAULT NULL,
    `sys_deleted` BIT(1) NOT NULL DEFAULT b'0',
    `sys_changedfrom` VARCHAR(50) NULL DEFAULT NULL,
    `sys_changedat` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    PRIMARY KEY (`id`),
    INDEX `artikel` (`artikel`) USING BTREE,
    INDEX `preisliste` (`preisliste`) USING BTREE,
    INDEX `einheit` (`einheit`) USING BTREE
)
COLLATE='utf8_general_ci'
ENGINE=InnoDB
AUTO_INCREMENT=2;

代码:

var units = from unit in context.Units
            where unit.Id == 4
            select unit;

foreach (var unit in units)
    Console.WriteLine(unit.Id + ": " + unit.Name); /* works */


var prices = from price in context.ArticlePrices
             select price;

foreach (var price in prices.ToList()) /* price.Unit = NULL ........... */
    MessageBox.Show(price.Article.Description + " " + price.Price + "/"
                    + price.Unit.Name + " in pricelist '"
                    + price.Pricelist.Name + "'");

谁能告诉我我做错了什么?

4

2 回答 2

1

您必须明确定义关系并为每个导航属性创建一个 xxId 属性,然后您可以使用相应的 ForeignKey 属性进行装饰。这里不需要“真正的”外键..

[Table("artikeldaten_preise")]
public class ArticlePrice : ClassicEntity
{
    [Key]
    [Column("id")]
    public int Id { get; set; }

    [Column("einheit")]
    public int UnitId { get; set; }

    [ForeignKey("UnitId")]
    public virtual Unit Unit { get; set; }

    [Column("preisliste")]
    public int PricelistId { get; set; }

    [ForeignKey("PricelistId")]
    public virtual Pricelist Pricelist { get; set; }

    [Column("artikel")]
    public int ArticleId { get; set; }

    [ForeignKey("ArticleId")]
    public virtual Article Article { get; set; }

    [Column("preis")]
    public double Price { get; set; }

}
于 2013-06-10T09:07:11.837 回答
0
  var prices = from price in context.ArticlePrices.Include("Price.Unit")
                     select price;
于 2013-06-07T13:51:42.290 回答