我有下表:
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。我试着调试,但是我没有找到存储外键的位置。请帮助使用TT模板:我修改的地点和内容。
答案 0 :(得分:0)
您可以通过在设计器中编辑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)));
}