Entity Framework TT 模板 -> 导航 属性 基于外键 ID 的名称
Entity Framework TT template -> Navigation Property name based on foreign key ID
我有以下 table:
CREATE TABLE Phone(
PhoneId INT IDENTITY (1, 1) NOT NULL,
CONSTRAINT PK_Phone PRIMARY KEY CLUSTERED (PhoneId ASC))
CREATE TABLE Person(
PersonId INT IDENTITY (1, 1) NOT NULL,
MobilePhoneId INT NOT NULL,
PhoneId INT NOT NULL,
...
CONSTRAINT PK_Person PRIMARY KEY CLUSTERED (PersonId ASC),
CONSTRAINT FK_Projects_Phone FOREIGN KEY (PhoneId)
REFERENCES Phone(PhoneId),
CONSTRAINT FK_Projects_MobileId FOREIGN KEY (MobilePhoneId)
REFERENCES Phone(PhoneId),
...
我正在使用 EF,我想根据外键生成导航属性,删除 Id 部分,所以我想要导航属性 Phone 和 MobilePhone。我尝试调试,但我没有找到外键在哪里 stored.Please 帮助 TT 模板:我应该在哪里和修改什么。
您可以通过在设计器中编辑edmx 或通过编辑T4 模板来完成。当你想编辑 T4 模板(你的模型 .tt 文件)时,尝试修改这个:
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)));
}
这样:
public string NavigationProperty(NavigationProperty navProp)
{
var navigationPropertyName = _code.Escape(navProp);
var match = System.Text.RegularExpressions.Regex.Match(navigationPropertyName, "^(?<a>.+)Id$");
if(match.Success)
navigationPropertyName = match.Groups[1].Value;
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,
navigationPropertyName,
_code.SpaceAfter(Accessibility.ForGetter(navProp)),
_code.SpaceAfter(Accessibility.ForSetter(navProp)));
}
我有以下 table:
CREATE TABLE Phone(
PhoneId INT IDENTITY (1, 1) NOT NULL,
CONSTRAINT PK_Phone PRIMARY KEY CLUSTERED (PhoneId ASC))
CREATE TABLE Person(
PersonId INT IDENTITY (1, 1) NOT NULL,
MobilePhoneId INT NOT NULL,
PhoneId INT NOT NULL,
...
CONSTRAINT PK_Person PRIMARY KEY CLUSTERED (PersonId ASC),
CONSTRAINT FK_Projects_Phone FOREIGN KEY (PhoneId)
REFERENCES Phone(PhoneId),
CONSTRAINT FK_Projects_MobileId FOREIGN KEY (MobilePhoneId)
REFERENCES Phone(PhoneId),
...
我正在使用 EF,我想根据外键生成导航属性,删除 Id 部分,所以我想要导航属性 Phone 和 MobilePhone。我尝试调试,但我没有找到外键在哪里 stored.Please 帮助 TT 模板:我应该在哪里和修改什么。
您可以通过在设计器中编辑edmx 或通过编辑T4 模板来完成。当你想编辑 T4 模板(你的模型 .tt 文件)时,尝试修改这个:
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)));
}
这样:
public string NavigationProperty(NavigationProperty navProp)
{
var navigationPropertyName = _code.Escape(navProp);
var match = System.Text.RegularExpressions.Regex.Match(navigationPropertyName, "^(?<a>.+)Id$");
if(match.Success)
navigationPropertyName = match.Groups[1].Value;
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,
navigationPropertyName,
_code.SpaceAfter(Accessibility.ForGetter(navProp)),
_code.SpaceAfter(Accessibility.ForSetter(navProp)));
}