2017-04-03 24 views
0

我不能FkModelloAutomobile插入idmodello值,智能感知请求ModelloAutomobile类型,而不是一个int类型:插入一个int值转换成ASP.NET MVC&C#的外键

ModelloAutomobile modello = new ModelloAutomobile(); 
int idmodello = Convert.ToInt32(form["ModelloId"].ToString()); 
modello = await service.GetProductByIdAsync(idmodello); 
Mappatura.FkModelloAutomobile = idmodello; 

Mappatura模型类:

public class Mappatura 
{ 
    [Key] 
    public int Id { get; set; } 

    [Required] 
    public string Applicazioni { get; set; } 

    [Required] 
    public string Settaggi { get; set; } 

    [Required] 
    public string Ecu { get; set; } 

    [Required] 
    public string Dettagli { get; set; } 

    [Required] 
    public decimal Costo { get; set; } 

    [Required] 
    public string DirectoryMappatura { get; set; } 

    [Required] 
    [ForeignKey("Id")] 
    public virtual ModelloAutomobile FkModelloAutomobile { get; set; } 
} 

ModelloAutomobile模型类:

public class ModelloAutomobile 
{ 
    [Key] 
    public int Id { get; set; } 

    [Required] 
    public string NomeModello { get; set; } 

    [Required] 
    public string TipoMotore { get; set; } 

    [Required] 
    public string Alimentazione { get; set; } 

    [Required] 
    public string Potenza { get; set; } 

    [Required] 
    public string Anno { get; set; } 

    [Required] 
    public virtual MarcaAutomobile FkMarcaAutomobile { get; set; } 
} 

回答

0

我相信你需要在Mappatura类的属性是这样的:

public int ModelloAutomobileId { get; set; } 

该属性映射到ModelloAutomobile类的ID属性。

+0

'code public int FkModelloAutomobileId {get;组; } [必须] [ForeignKey(“FkModelloAutomobileId”)] public virtual ModelloAutomobile FkModelloAutomobile {get;组; }' –

+0

谢谢你!我刚加入'[ForeignKey(“FkModelloAutomobileId”)]' –