我修复了所有主键,提供了更新数据库,但出现以下错误:
我试图通过建立外键来修复它,但是我有一个新错误:
类型为'Proiect_masini_firma_taxi.Models.Masini'的属性'ID_Model'上的ForeignKeyAttribute无效。在相关类型“ Proiect_masini_firma_taxi.Models.Masini”上找不到导航属性“ Modele_masini”。名称值应为有效的导航属性名称。
Masini.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;
namespace Proiect_masini_firma_taxi.Models
{
public enum Da_Sau_Nu
{
Da, Nu
}
public class Masini
{
[Key]
public int ID_Masina { get; set; }
public string Numar_inmatriculare { get; set; }
[ForeignKey("Modele_masini")]
public int ID_Model { get; set; }
public int An_Fabricatie { get; set; }
public int ID_proprietar { get; set; }
public Da_Sau_Nu Disponibilitate { get; set; }
public Soferi Soferi_masini { get; set; }
public Ture Ture_masini { get; set; }
public Modele_masini Masini_modele { get; set; }
internal static void ForEach(Func<object, object> p)
{
throw new NotImplementedException();
}
}
}
Modele_masini.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;
namespace Proiect_masini_firma_taxi.Models
{
public class Modele_masini
{
[Key]
public int ID_model { get; set; }
public string Nume_model { get; set; }
public string Descriere_Model { get; set; }
public ICollection<Masini> Masini_modele { get; set; }
}
}
Soferi.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;
namespace Proiect_masini_firma_taxi.Models
{
public enum DaSauNu
{
Da, Nu
}
public class Soferi
{
[Key]
public int ID_sofer { get; set; }
public string Nume { get; set; }
public string Prenume { get; set; }
public DateTime Data_nasterii { get; set; }
public string Serie_permis_de_conducere { get; set; }
public DateTime Data_Expirarii { get; set; }
public DaSauNu Angajat_curent { get; set; }
public ICollection<Ture> Soferi_ture { get; set; }
public ICollection<Masini> Soferi_masini { get; set; }
internal static void ForEach(Func<object, object> p)
{
throw new NotImplementedException();
}
}
}
Ture.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;
namespace Proiect_masini_firma_taxi.Models
{
public class Ture
{
[Key]
public int ID_tura { get; set; }
[ForeignKey("Soferi")]
public int ID_sofer { get; set; }
[ForeignKey("Masini")]
public int ID_masina { get; set; }
public DateTime Moment_Start_Tura { get; set; }
public DateTime Moment_Sfarsit_Tura { get; set; }
public DateTime Moment_logare { get; set; }
public DateTime Moment_delogare { get; set; }
public Ture Ture_Sofer { get; set; }
public Masini Ture_Masini { get; set; }
}
}
答案 0 :(得分:0)
问题在于[ForeignKey("Modele_masini")]
中的导航属性名称与public Modele_masini Masini_modele { get; set; }
中的导航属性名称不匹配。
因此,请按照以下步骤更新您的Masini
mdoel类:
public class Masini
{
[Key]
public int ID_Masina { get; set; }
public string Numar_inmatriculare { get; set; }
[ForeignKey("Masini_modele")]
public int ID_Model { get; set; }
public int An_Fabricatie { get; set; }
public int ID_proprietar { get; set; }
public Da_Sau_Nu Disponibilitate { get; set; }
public Soferi Soferi_masini { get; set; }
public Ture Ture_masini { get; set; }
public Modele_masini Masini_modele { get; set; }
internal static void ForEach(Func<object, object> p)
{
throw new NotImplementedException();
}
}
您的Ture
模型类中也有同样的问题。所以也如下更新:
public class Ture
{
[Key]
public int ID_tura { get; set; }
[ForeignKey("Ture_Sofer")]
public int ID_sofer { get; set; }
[ForeignKey("Ture_Masini")]
public int ID_masina { get; set; }
public DateTime Moment_Start_Tura { get; set; }
public DateTime Moment_Sfarsit_Tura { get; set; }
public DateTime Moment_logare { get; set; }
public DateTime Moment_delogare { get; set; }
public Ture Ture_Sofer { get; set; }
public Masini Ture_Masini { get; set; }
}
希望这可以解决问题!