如何确定PropertyType是否为外键

编程入门 行业动态 更新时间:2024-10-20 20:33:40
本文介绍了如何确定PropertyType是否为外键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我有以下类别'schakeling',用EF生成,代表一个数据库表'schakeling'。在数据库中'id'是主键,'plc_id'是外键。

public partial class schakeling { public schakeling() { this.verbruik = new HashSet< verbruik>(); } public int id {get;组; } public int plc_id {get;组; } public string var_output {get;组; } public string omschrijving {get;组; } public int vermogen {get;组; } public Nullable< bool>状态{get;组; } public Nullable< byte>小时组; } public Nullable< byte> minOn {get;组; } public Nullable< byte>小时{get;组; } public Nullable< byte>分钟组; } public virtual plc plc {get;组; } public virtual ICollection< verbruik> verbruik {get;组; }

我有一个视图类

public class SchakelingsListViewModel { public IEnumerable< schakeling>列表{get;组; } public PagingInfo PagingInfo {get;组; } //与此问题无关 //还有一些属性...也不相关}

我有以下视图(为简洁起见省略一些HTML)

@model PortalControl.ViewModels.SchakelingsListViewModel < h2>索引< / h2> < table> @foreach(Model.List中的var项){ @ Html.TableRowFor(item)} < / table>

我有一个通用的html帮助程序TableRowFor,因为我想要能够使用其他域上的方法用EF生成的实体。该方法生成简单的表数据。

public static MvcHtmlString TableRowFor< T>(此HtmlHelper助手,T obj) { string controller = obj.GetType()。BaseType.Name; string id = obj.GetType()。GetProperty(id)。GetValue(obj).ToString(); StringBuilder sb = new StringBuilder(< tr>); sb.Append(< td>); sb.Append(< a href ='+ controller +/ Edit /+ id +'>< img src ='/ Images / edit-icon.png'/>< ; / A>中); sb.Append(< a href ='+ controller +/ Details /+ id +'>< img src ='/ Images / details-icon.png'/>< ; / A>中); sb.Append(< a href ='+ controller +/ Delete /+ id +'>< img src ='/ Images / delete-icon.png'/>< ; / A>中); sb.Append(< / td>); foreach(obj.GetType()中的var属性GetProperties()) { //如果下面的语句过滤出两个虚拟属性(plc,verbruik) schakeling类(如所说,用EF生成),有些丑陋,但它的作品,更好的建议是欢迎.. if((!property.PropertyType.Name.ToLower()。Contains(icollection))& &(property.PropertyType.CustomAttributes.Count()!= 0)) { sb.Append(< td>); // if property is foreign key //sb.Append(\"<a href ='+ correctControllerNameHere +/ Details /+ property.GetValue(obj)+'>< ; img src ='/ Images / details-icon.png'/>< / a>); // else //sb.Append(property.GetValue(obj)); sb.Append(< / td>); } } sb.Append(< / tr>); 返回新的MvcHtmlString(sb.ToString()); }

我的问题是,我想创建一个链接如果一个财产是一个外键。

我已经搜索互联网,但我不是PropertyInfo,MetaDataClassType和其他类似的类的专家。像property.isForeign()将是可爱的,但任何有用的东西将不胜感激。

解决方案

您可以从通过这种方法实体框架概念模型:

IEnumerable< string> GetReferenceProperies< T>(DbContext context) { var oc =((IObjectContextAdapter)context).ObjectContext; var entityType = oc.MetadataWorkspace.GetItems(DataSpace.OSpace) .OfType< EntityType>() .FirstOrDefault(et = et etNameName == typeof(T).Name ); if(entityType!= null) { foreach(NavigationProperty np in entityType.NavigationProperties .Where(p => p.ToEndMember.RelationshipMultiplicity == RelationshipMultiplicity.One || p.ToEndMember.RelationshipMultiplicity == RelationshipMultiplicity.ZeroOrOne)) { yield return np.Name; } } }

它获取所有导航属性在协会结尾处有 0..1 ,以便排除收集导览属性。

现在你可以使用属性名称获取匹配的 PropertyInfo s并获取属性的值。

I have the following class 'schakeling', generated with EF, representing a database table 'schakeling'. In the database 'id' is the primary key and 'plc_id' is a foreign key.

public partial class schakeling { public schakeling() { this.verbruik = new HashSet<verbruik>(); } public int id { get; set; } public int plc_id { get; set; } public string var_output { get; set; } public string omschrijving { get; set; } public int vermogen { get; set; } public Nullable<bool> status { get; set; } public Nullable<byte> hourOn { get; set; } public Nullable<byte> minuteOn { get; set; } public Nullable<byte> hourOff { get; set; } public Nullable<byte> minuteOff { get; set; } public virtual plc plc { get; set; } public virtual ICollection<verbruik> verbruik { get; set; } }

I have a view class

public class SchakelingsListViewModel { public IEnumerable<schakeling> List { get; set; } public PagingInfo PagingInfo { get; set; }//Not relevant for this question //And some more properties...also not relevant }

I have the following view (omitted some HTML for brevity)

@model PortalControl.ViewModels.SchakelingsListViewModel <h2>Index</h2> <table> @foreach (var item in Model.List) { @Html.TableRowFor(item) } </table>

I have a generic html helper method TableRowFor because I want to be able to use the method on other domain entities generated with EF. The method generates simple table data.

public static MvcHtmlString TableRowFor<T>(this HtmlHelper helper, T obj) { string controller = obj.GetType().BaseType.Name; string id = obj.GetType().GetProperty("id").GetValue(obj).ToString(); StringBuilder sb = new StringBuilder("<tr>"); sb.Append("<td>"); sb.Append("<a href='" + controller + "/Edit/" + id + "'><img src='/Images/edit-icon.png' /></a>"); sb.Append("<a href='" + controller + "/Details/" + id + "'><img src='/Images/details-icon.png' /></a>"); sb.Append("<a href='" + controller + "/Delete/" + id + "'><img src='/Images/delete-icon.png' /></a>"); sb.Append("</td>"); foreach (var property in obj.GetType().GetProperties()) { //If statement below filters out the two virtual properties(plc, verbruik) of the schakeling class(as said, generated with EF), somewhat ugly but it works, better suggestions are welcome.. if ((!property.PropertyType.Name.ToLower().Contains("icollection")) && (property.PropertyType.CustomAttributes.Count() != 0)) { sb.Append("<td>"); //if property is foreign key //sb.Append("<a href='" + correctControllerNameHere + "/Details/" + property.GetValue(obj) + "'><img src='/Images/details-icon.png' /></a>"); //else //sb.Append(property.GetValue(obj)); sb.Append("</td>"); } } sb.Append("</tr>"); return new MvcHtmlString(sb.ToString()); }

The question I have is, I would like to create a link if a property is a foreign key.

I have searched the internet but I am no expert in PropertyInfo, MetaDataClassType and other similar classes. Something like property.isForeign() would be lovely but anything that works will be appreciated.

解决方案

You can get reference navigation properties from the Entity Framework conceptual model by this method:

IEnumerable<string> GetReferenceProperies<T>(DbContext context) { var oc = ((IObjectContextAdapter)context).ObjectContext; var entityType = oc.MetadataWorkspace.GetItems(DataSpace.OSpace) .OfType<EntityType>() .FirstOrDefault (et => et.Name == typeof(T).Name); if (entityType != null) { foreach (NavigationProperty np in entityType.NavigationProperties .Where(p => p.ToEndMember.RelationshipMultiplicity == RelationshipMultiplicity.One || p.ToEndMember.RelationshipMultiplicity == RelationshipMultiplicity.ZeroOrOne)) { yield return np.Name; } } }

It gets all navigation properties that have 0..1 at the end of the association, so that excludes collection navigation properties.

Now you can use the property names to get the matching PropertyInfos and get the value of the properties.

更多推荐

如何确定PropertyType是否为外键

本文发布于:2023-11-29 17:15:42,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1647062.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:PropertyType

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!