繁体   English   中英

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

[英]Entity Framework buddy class results in type conversion error

我一直在尝试建立一个伙伴类( 在此答案中进行描述 ),因此每次在数据库中更新模型时,在自动生成的Entity Framework类上设置的注释都不会丢失。

我在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; }
        }
    }
}

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM