我在这里搜索了很多问题,听起来与我的相似,但如果它与我的问题相匹配,则没有一个,所以我希望对这张票有所帮助..
我使用 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 + "'");
谁能告诉我我做错了什么?