实体框架伙伴类导致类型转换错误

时间:2016-09-16 17:11:12

标签: c# entity-framework oop

我一直在尝试设置一个好友类(described in this answer),因此我在自动生成的Entity Framework类上设置的注释不会在每次从数据库更新模型时丢失。

我在MVC项目的Models目录中创建了buddy类,EDMX在解决方案的另一个项目中。它无法使用此错误进行编译:

错误CS0029:无法隐式转换类型' TrinityCatalogTool.Data.Details [C:\ Projects \ Bitbucket \ catalog-tool \ TrinityCatalogTool.Data \ bin \ Debug \ TrinityCatalogTool.Data.dll]&# 39; to' TrinityCatalogTool.Data.Details [C:\ Projects \ Bitbucket \ catalog-tool \ TrinityCatalogTool \ Models \ Metadata.cs(9)]' (112,35)

我不明白为什么它会把原来的课程投给我的伙伴班,因为伙伴班是原版的一部分。知道我做错了吗?

这是我自动生成的类的样子:

//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated from a template.
//
//     Manual changes to this file may cause unexpected behavior in your application.
//     Manual changes to this file will be overwritten if the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

namespace TrinityCatalogTool.Data
{
    using System;
    using System.Collections.Generic;

    public partial class Details
    {
        public int detail_id { get; set; }
        public int parent_id { get; set; }
        public string short_description { get; set; }
        public string long_description { get; set; }
        public string feature1 { get; set; }
        public string feature2 { get; set; }
        public string feature3 { get; set; }
        public string feature4 { get; set; }
        public string feature5 { get; set; }
        public string feature6 { get; set; }
        public string feature7 { get; set; }
        public string feature8 { get; set; }

        public virtual Parents Parents { get; set; }
    }
}

这就是我创建的好友课程看起来像

using System.ComponentModel.DataAnnotations;

namespace TrinityCatalogTool.Data
{
    [MetadataType(typeof(Details.Metadata))]
    public partial class Details
    {
        private sealed class Metadata
        {
            [Display(Name = "Short Description")]
            public string short_description { get; set; }
            [Display(Name = "Long Description")]
            public string long_description { get; set; }
            [Display(Name = "Feature #1")]
            public string feature1 { get; set; }
            [Display(Name = "Feature #2")]
            public string feature2 { get; set; }
            [Display(Name = "Feature #3")]
            public string feature3 { get; set; }
            [Display(Name = "Feature #4")]
            public string feature4 { get; set; }
            [Display(Name = "Feature #5")]
            public string feature5 { get; set; }
            [Display(Name = "Feature #6")]
            public string feature6 { get; set; }
            [Display(Name = "Feature #7")]
            public string feature7 { get; set; }
            [Display(Name = "Feature #8")]
            public string feature8 { get; set; }
        }
    }
}

1 个答案:

答案 0 :(得分:1)

Per Siva Gopal的评论,问题是我的部分类需要与我的自动生成的类存在于同一个项目中。当我将好友类移动到与原始类相同的项目时,它按预期编译和工作。