【问题标题】:Entity Framework buddy class results in type conversion errorEntity Framework buddy 类导致类型转换错误
【发布时间】:2016-09-16 17:11:12
【问题描述】:

我一直在尝试设置一个伙伴类 (described in this answer),因此我在自动生成的实体框架类上设置的注释不会在每次我从数据库中更新模型时丢失。

我在我的 MVC 项目的 Models 目录中创建了伙伴类,EDMX 在解决方案的另一个项目中。它无法编译并出现此错误:

错误 CS0029:无法将类型“TrinityCatalogTool.Data.Details [C:\Projects\Bitbucket\catalog-tool\TrinityCatalogTool.Data\bin\Debug\TrinityCatalogTool.Data.dll]”隐式转换为“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; }
        }
    }
}

【问题讨论】:

  • this 是否为您的问题提供提示?
  • 谢谢,你一针见血。我将我的伙伴类移到与我的实体框架模型相同的项目中,它现在可以工作了。

标签: c# entity-framework oop


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2014-07-01
    • 2013-05-14
    • 1970-01-01
    • 2015-12-04
    • 1970-01-01
    • 2020-01-20
    • 1970-01-01
    • 2017-09-26
    • 1970-01-01
    相关资源
    最近更新 更多