我是Ado网的新手。我正在使用EF 6。
我有2个表UserInfo和UserType。
现在,当我想获取userInfo时,我想将UserTypeName设置为与其他UserInfo一起显示在网格中,为此我使用了下面的代码。
public List<UserInfoBind> SearchBindUser(UserInfo objSeacrh)
{
try
{
List<UserInfoBind> objList = (from sql in dbContext.UserInfoes.Where(t => t.IsDelete != true)
join tblType in dbContext.UserTypes
on sql.UserType equals tblType.UserTypeID
select new UserInfoBind()
{
UserTypeName = tblType.UserTypeName
}).ToList();
return objList;
}
catch (Exception ex)
{
throw ex;
}
}
和Decalre UserInforBind如下:
[DataContract]
public class UserInfoBind : UserInfo
{
public UserInfoBind()
{
}
[DataMember]
public string UserTypeName { get; set; }
}
但它给出了错误:&#34;输入&#39; DAL.UserInfoBind&#39;不能从未标记为DataContractAttribute或SerializableAttribute的类型继承。&#34;
那么如何在EF 6中创建Userinfo表实体的SerializableAttribute。 谢谢, Hitesh Paghadal
答案 0 :(得分:1)
有两种方法可以达到你想要的效果。
如果要使用用于表示WCF DataContract的数据库模型的相同类型,则需要使用DataContract属性标记实体,并使用DataMember属性标记属性。我假设您正在使用Database First方法,如果是,您可以更改代码生成模板以使用DataContract属性标记所有实体,并使用DataMember属性标记所有属性。
另一种方法是保持数据库类型不变,为DataContract创建新的类型集,并使用AutoMapper等工具映射所需的属性。
我更喜欢第二种方法,因为它允许您更改底层模型,而不会影响通过WCF服务公开的内容。
修改:如果您想使用编辑代码模板,那么就是这样做的
在您的EDMX文件中,您将看到两个扩展名为.tt的文件。一个用于数据上下文,一个用于模型。 打开一个create for model并进行以下更改
// To add DataMember attibute in all the properties
// Change
public string Property(EdmProperty edmProperty)
{
return string.Format(
CultureInfo.InvariantCulture,
"{0} {1} {2} {{ {3}get; {4}set; }}",
Accessibility.ForProperty(edmProperty),
_typeMapper.GetTypeName(edmProperty.TypeUsage),
_code.Escape(edmProperty),
_code.SpaceAfter(Accessibility.ForGetter(edmProperty)),
_code.SpaceAfter(Accessibility.ForSetter(edmProperty)));
}
// To
public string Property(EdmProperty edmProperty)
{
return string.Format(
CultureInfo.InvariantCulture,
"[DataMember] {0} {1} {2} {{ {3}get; {4}set; }}",
Accessibility.ForProperty(edmProperty),
_typeMapper.GetTypeName(edmProperty.TypeUsage),
_code.Escape(edmProperty),
_code.SpaceAfter(Accessibility.ForGetter(edmProperty)),
_code.SpaceAfter(Accessibility.ForSetter(edmProperty)));
}
// To add DataMember attibute in all the navigation properties
// Change
public string NavigationProperty(NavigationProperty navProp)
{
var endType = _typeMapper.GetTypeName(navProp.ToEndMember.GetEntityType());
return string.Format(
CultureInfo.InvariantCulture,
"{0} {1} {2} {{ {3}get; {4}set; }}",
AccessibilityAndVirtual(Accessibility.ForNavigationProperty(navProp)),
navProp.ToEndMember.RelationshipMultiplicity == RelationshipMultiplicity.Many ? ("ICollection<" + endType + ">") : endType,
_code.Escape(navProp),
_code.SpaceAfter(Accessibility.ForGetter(navProp)),
_code.SpaceAfter(Accessibility.ForSetter(navProp)));
}
// TO
public string NavigationProperty(NavigationProperty navProp)
{
var endType = _typeMapper.GetTypeName(navProp.ToEndMember.GetEntityType());
return string.Format(
CultureInfo.InvariantCulture,
"[DataMember] {0} {1} {2} {{ {3}get; {4}set; }}",
AccessibilityAndVirtual(Accessibility.ForNavigationProperty(navProp)),
navProp.ToEndMember.RelationshipMultiplicity == RelationshipMultiplicity.Many ? ("ICollection<" + endType + ">") : endType,
_code.Escape(navProp),
_code.SpaceAfter(Accessibility.ForGetter(navProp)),
_code.SpaceAfter(Accessibility.ForSetter(navProp)));
}
// To add DataContract attibute in all the types
// Change
public string EntityClassOpening(EntityType entity)
{
return string.Format(
CultureInfo.InvariantCulture,
"{0} {1}partial class {2}{3}",
Accessibility.ForType(entity),
_code.SpaceAfter(_code.AbstractOption(entity)),
_code.Escape(entity),
_code.StringBefore(" : ", _typeMapper.GetTypeName(entity.BaseType)));
}
// To
public string EntityClassOpening(EntityType entity)
{
return string.Format(
CultureInfo.InvariantCulture,
"[DataContract] {0} {1}partial class {2}{3}",
Accessibility.ForType(entity),
_code.SpaceAfter(_code.AbstractOption(entity)),
_code.Escape(entity),
_code.StringBefore(" : ", _typeMapper.GetTypeName(entity.BaseType)));
}
// To include System.Runtime.Serialization
// Change
public string UsingDirectives(bool inHeader, bool includeCollections = true)
{
return inHeader == string.IsNullOrEmpty(_code.VsNamespaceSuggestion())
? string.Format(
CultureInfo.InvariantCulture,
"{0}using System;{1}" +
"{2}",
inHeader ? Environment.NewLine : "",
includeCollections ? (Environment.NewLine + "using System.Collections.Generic;") : "",
inHeader ? "" : Environment.NewLine)
: "";
}
// To
public string UsingDirectives(bool inHeader, bool includeCollections = true)
{
return inHeader == string.IsNullOrEmpty(_code.VsNamespaceSuggestion())
? string.Format(
CultureInfo.InvariantCulture,
"{0}using System;{1}" +
"using System.Runtime.Serialization;{2}" +
"{3}",
inHeader ? Environment.NewLine : "",
Environment.NewLine,
includeCollections ? (Environment.NewLine + "using System.Collections.Generic;") : "",
inHeader ? "" : Environment.NewLine)
: "";
}
编辑2 :更改您的select方法以包含UserInfo中的所有属性,否则您的UserInfoBind将只有UserTypeName
select new UserInfoBind()
{
userInfoprop1 = sql.value1,
userInfoprop2 = sql.value2,
UserTypeName = tblType.UserTypeName
}).ToList();