- 增加 [Navigate(xx, TempPrimary = xx)] 与非主键关联;(仅支持查询)

This commit is contained in:
2881099
2023-03-02 15:46:45 +08:00
parent c6556a47ab
commit da7bb7c74d
14 changed files with 243 additions and 101 deletions

View File

@ -22,6 +22,13 @@ namespace FreeSql.DataAnnotations
/// _________________public List&lt;Topic&gt; Topics { get; set; }<para></para>
/// </summary>
public string Bind { get; set; }
/// <summary>
/// 与非主键进行关联,仅支持 OneToMany、ManyToOne<para></para>
/// 使用方法参考 Bind 属性
/// </summary>
public string TempPrimary { get; set; }
/// <summary>
/// 手工绑定 ManyToMany 导航关系
/// </summary>

View File

@ -60,10 +60,12 @@ namespace FreeSql.DataAnnotations
/// <param name="bind"></param>
/// <param name="manyToMany">多对多关系的中间实体类型</param>
/// <returns></returns>
public TableFluent Navigate(string proto, string bind, Type manyToMany = null)
public TableFluent Navigate(string proto, string bind, Type manyToMany = null) => NavigateInternal(proto, bind, null, manyToMany);
public TableFluent Navigate(string proto, string bind, string tempPrimary) => NavigateInternal(proto, bind, tempPrimary, null);
TableFluent NavigateInternal(string proto, string bind, string tempPrimary, Type manyToMany)
{
if (_properties.TryGetValue(proto, out var tryProto) == false) throw new KeyNotFoundException(CoreStrings.NotFound_Property(proto));
var nav = new NavigateAttribute { Bind = bind, ManyToMany = manyToMany };
var nav = new NavigateAttribute { Bind = bind, TempPrimary = tempPrimary, ManyToMany = manyToMany };
_table._navigates.AddOrUpdate(tryProto.Name, nav, (name, old) => nav);
return this;
}
@ -148,18 +150,22 @@ namespace FreeSql.DataAnnotations
/// <param name="bind"></param>
/// <param name="manyToMany">多对多关系的中间实体类型</param>
/// <returns></returns>
public TableFluent<T> Navigate<TProto>(Expression<Func<T, TProto>> proto, string bind, Type manyToMany = null)
public TableFluent<T> Navigate<TProto>(Expression<Func<T, TProto>> proto, string bind, Type manyToMany = null) => NavigateInternal(proto, bind, null, manyToMany);
public TableFluent<T> Navigate<TProto>(Expression<Func<T, TProto>> proto, string bind, string tempPrimary) => NavigateInternal(proto, bind, tempPrimary, null);
TableFluent<T> NavigateInternal<TProto>(Expression<Func<T, TProto>> proto, string bind, string tempPrimary, Type manyToMany = null)
{
var exp = proto?.Body;
if (exp.NodeType == ExpressionType.Convert) exp = (exp as UnaryExpression)?.Operand;
var member = (exp as MemberExpression)?.Member;
if (member == null) throw new FormatException(CoreStrings.Bad_Expression_Format(proto));
return Navigate(member.Name, bind, manyToMany);
return NavigateInternal(member.Name, bind, tempPrimary, manyToMany);
}
public TableFluent<T> Navigate(string proto, string bind, Type manyToMany = null)
public TableFluent<T> Navigate(string proto, string bind, Type manyToMany = null) => NavigateInternal(proto, bind, null, manyToMany);
public TableFluent<T> Navigate(string proto, string bind, string tempPrimary) => NavigateInternal(proto, bind, tempPrimary, null);
TableFluent<T> NavigateInternal(string proto, string bind, string tempPrimary, Type manyToMany)
{
if (_properties.TryGetValue(proto, out var tryProto) == false) throw new KeyNotFoundException(CoreStrings.NotFound_PropertyName(proto));
var nav = new NavigateAttribute { Bind = bind, ManyToMany = manyToMany };
var nav = new NavigateAttribute { Bind = bind, TempPrimary = tempPrimary, ManyToMany = manyToMany };
_table._navigates.AddOrUpdate(tryProto.Name, nav, (name, old) => nav);
return this;
}