实体框架伙伴类导致类型转换错误
本文关键字:类型转换 错误 框架 伙伴 实体 | 更新日期: 2023-09-27 18:07:20
我一直在尝试设置一个好友类(在这个答案中描述),所以我在自动生成的实体框架类上设置的注释不会在每次从数据库更新模型时丢失。
我在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)
我不明白为什么将原始类强制转换为我的buddy类会失败,因为buddy类是原始类的部分。知道我做错了什么吗?
这是我自动生成的类的样子:
//------------------------------------------------------------------------------
// <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的评论,问题是我的部分类需要与自动生成的类存在于同一个项目中。当我将buddy类移动到与原始类相同的项目中时,它按预期编译并工作。