Skip to content

Commit

Permalink
add support for one to one relation
Browse files Browse the repository at this point in the history
  • Loading branch information
Cinorid committed Jun 2, 2023
1 parent 7ceacd2 commit 4083db5
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 3 deletions.
1 change: 1 addition & 0 deletions Dbml2Ef/DBML/Association.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ public class Association
public string? Member { get; set; }
public string? ThisKey { get; set; }
public string? OtherKey { get; set; }
public string? Cardinality { get; set; }

public override string ToString()
{
Expand Down
2 changes: 1 addition & 1 deletion Dbml2Ef/Dbml2Ef.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<PackAsTool>true</PackAsTool>
<AssemblyVersion>0.1.5</AssemblyVersion>
<AssemblyName>dbml2ef</AssemblyName>
<TargetFrameworks>net5.0;net6.0;netcoreapp3.1</TargetFrameworks>
<TargetFrameworks>net6.0;net5.0;netcoreapp3.1</TargetFrameworks>
</PropertyGroup>

<PropertyGroup>
Expand Down
12 changes: 10 additions & 2 deletions Dbml2Ef/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,11 +133,18 @@ private static void GenerateModelClass(Database dbModelDatabase, string namespac
{
if (association.IsForeignKey)
{
strBuilder.AppendLine($"{namespaceIndentation} public virtual {association.Type} {association.Member} {{ get; set; }} = null!;");
strBuilder.AppendLine($"{namespaceIndentation} public virtual {association.Type} {(association.ThisKey == association.OtherKey ? "IdNavigation" : association.Member)} {{ get; set; }} = null!;");
}
else
{
strBuilder.AppendLine($"{namespaceIndentation} public virtual ICollection<{association.Type}> {association.Member} {{ get; set; }} = new List<{association.Type}>();");
if (association.Cardinality == "One")
{
strBuilder.AppendLine($"{namespaceIndentation} public virtual {association.Type}? {association.Type} {{ get; set; }}");
}
else
{
strBuilder.AppendLine($"{namespaceIndentation} public virtual ICollection<{association.Type}> {association.Member} {{ get; set; }} = new List<{association.Type}>();");
}
}

strBuilder.AppendLine("");
Expand Down Expand Up @@ -289,6 +296,7 @@ private static DBML.Database ConvertXElementToDatabase(XElement content)
association.ThisKey = elementColumn?.Attribute("ThisKey")?.Value;
association.OtherKey = elementColumn?.Attribute("OtherKey")?.Value;
association.Member = elementColumn?.Attribute("Member")?.Value;
association.Cardinality = elementColumn?.Attribute("Cardinality")?.Value;

var isForeignKey = elementColumn?.Attribute("IsForeignKey")?.Value;
association.IsForeignKey = Convert.ToBoolean(isForeignKey ?? false.ToString());
Expand Down

0 comments on commit 4083db5

Please sign in to comment.