C# 实体框架6 MySQL-未正确插入属性

C# 实体框架6 MySQL-未正确插入属性,c#,entity-framework-6,C#,Entity Framework 6,我将EF6与MySQL结合使用,并使用通过JSON解析创建的电影填充数据库 这是我的电影课: [JsonObject(MemberSerialization.OptIn)] [Table("movies")] public class MovieJSON { [Key] public int Id { get; set; } [JsonProperty(PropertyName = "Title")] [Column("title")] public S

我将EF6与MySQL结合使用,并使用通过JSON解析创建的电影填充数据库

这是我的电影课:

[JsonObject(MemberSerialization.OptIn)]
[Table("movies")]
public class MovieJSON
{
    [Key]
    public int Id { get; set; }

    [JsonProperty(PropertyName = "Title")]
    [Column("title")]
    public String Title { get; set; }

    [JsonProperty(PropertyName = "Year")]
    [Column("year")]
    public int Year { get; set; }

    [JsonProperty(PropertyName = "Genre")]
    [Column("genre")]
    public String Genre { get; set; }

    [JsonProperty(PropertyName = "Director")]
    [Column("director")]
    public String Director { get; set; }

    [JsonProperty(PropertyName = "Actors")]
    [Column("actors")]
    public String Actors { get; set; }

    [JsonProperty(PropertyName = "Plot")]
    [Column("plot")]
    public String Plot { get; set; }

    [JsonProperty(PropertyName = "Poster")]
    [Column("poster")]
    public String Poster { get; set; }

    [JsonProperty(PropertyName = "Metascore")]
    public String Metascore { get; set; }

    [Column("metascore")]
    public int MetascoreInt
    {
        get
        {
            int result;
            if (int.TryParse(Metascore, NumberStyles.AllowThousands, CultureInfo.InvariantCulture, out result))
                return result;
            return 0;
        }
    }

    [JsonProperty(PropertyName = "imdbRating")]
    public String ImdbRating { get; set; }

    [Column("imdb_rating")]
    public Decimal ImdbRatingDecimal
    {
        get
        {
            Decimal result;
            if (Decimal.TryParse(ImdbRating, out result))
                return result;
            return 0;
        }
    }

    [JsonProperty(PropertyName = "imdbVotes")]
    public String ImdbVotes { get; set; }

    [Column("imdb_votes")]
    public long ImdbVotesLong
    {
        get
        {
            long result;
            String stringToParse = ImdbVotes.Remove(ImdbVotes.IndexOf(','), 1);

            if (long.TryParse(stringToParse, out result))
                return result;
            return 0;
        }
    }

    [JsonProperty(PropertyName = "imdbID")]
    [Column("imdb_id")]
    public String ImdbID { get; set; }

    [JsonProperty(PropertyName = "type")]
    [Column("type")]
    public String Type { get; set; }

    public override string ToString()
    {
        String[] propertiesToIgnore = {"MetascoreInt", "ImdbRatingDecimal", "ImdbVotesLong"};
        var sb = new StringBuilder();

        PropertyInfo[] properties = GetType().GetProperties();

        foreach (PropertyInfo propertyInfo in properties)
        {
            if (propertiesToIgnore.Contains(propertyInfo.Name))
                continue;

            sb.AppendLine(String.Format("{0} : {1} ",
                propertyInfo.Name, propertyInfo.GetValue(this, null)));
        }

        return sb.ToString();
    }
}
这是我的EF6配置上下文类(我将忽略字符串字段,而是使用帮助器字段,因为数据库配置为接受Metascore的int等):

模型创建时受保护的覆盖无效(DbModelBuilder modelBuilder)
{
modelBuilder.Entity().Ignore(e=>e.Metascore).Ignore(e=>e.ImdbRating).Ignore(e=>e.ImdbRating);
基于模型创建(modelBuilder);
}
其他图像信息:

插入数据库之前的对象值(所有值都已正确设置)

数据库中的值(忽略重复数据):

我不明白为什么我在数值域中得到零

任何帮助都将不胜感激!:)


最好的

试试这个,其他的
数值属性
也一样,您不希望字符串
JSON
属性映射到
数据库

private string _metaScore;
[JsonProperty(PropertyName = "Metascore")]
public String Metascore { 
get { return _metaScore; } 
set { _metaScore = value; 
      int result;
      if(int.TryParse(_metaScore, NumberStyles.AllowThousands, CultureInfo.InvariantCulture, out result))
       MetaScoreInt = result;
    }
}

[Column("metascore")]
public int MetascoreInt
{
    get;set;
}

我的猜测是因为您仍在使用您试图覆盖的相同名称构建Metascore“string”列。既然希望int字段进入db,那么可以在string字段上使用
NotMapped
属性吗?嗯,我的modelBuilder上的“Ignore”方法不应该足够跳过它们吗?此外,imdb_评级和imdb_投票数为零,即使这些列通过[Column]属性显式映射到非字符串字段等效项。EF可以将ImdbRating字段解析为imdb_rating列吗?这是我不确定的,因为您试图解析“string”列中的值以获得int值(这意味着这些列也没有setter)。
private string _metaScore;
[JsonProperty(PropertyName = "Metascore")]
public String Metascore { 
get { return _metaScore; } 
set { _metaScore = value; 
      int result;
      if(int.TryParse(_metaScore, NumberStyles.AllowThousands, CultureInfo.InvariantCulture, out result))
       MetaScoreInt = result;
    }
}

[Column("metascore")]
public int MetascoreInt
{
    get;set;
}