EntityTypeConfiguration如何获取关键属性?

2024-01-14

我正在使用 C#、EF 5 和 MVC4 我正在使用 EntityTypeConfiguration 设置主键和后端表

  public class AuditZoneMap : EntityTypeConfiguration<AuditZone>
    {
        public AuditZoneMap()
        {
            // Primary Key
            HasKey(t => t.Id);


            // Properties
            Property(t => t.Description)
                .HasMaxLength(100);


            // Table & Column Mappings
            ToTable("AuditZone");
            Property(t => t.Id).HasColumnName("Id");
            Property(t => t.Description).HasColumnName("Description");
            Property(t => t.Valid).HasColumnName("Valid");          
            Property(t => t.CreatedDate).HasColumnName("CreatedDate");
            Property(t => t.CreatedBy).HasColumnName("CreatedBy");
            Property(t => t.ModifiedDate).HasColumnName("ModifiedDate");
            Property(t => t.ModifiedBy).HasColumnName("ModifiedBy");

            // Relationships        
            HasOptional(t => t.CreatedByUser)
               .WithMany(t => t.CreatedByAuditZone)
               .HasForeignKey(d => d.CreatedBy);

            HasOptional(t => t.ModifiedByUser)
                .WithMany(t => t.ModifiedByAuditZone)
                .HasForeignKey(d => d.ModifiedBy);


        }
    }

我的模型定义如下,

    public class AuditZone
    {
        public AuditZone()
        {
            AuditZoneUploadedCOESDetails = new List<UploadedCOESDetails>();
            AuditZonePostcode = new List<Postcodes>();
        }


        public int Id { get; set; }
        public string Description { get; set; }     
        public bool Valid { get; set; }       
        public DateTime CreatedDate { get; set; }
        public int? CreatedBy { get; set; }
        public DateTime? ModifiedDate { get; set; }
        public int? ModifiedBy { get; set; }

        public virtual UserProfile CreatedByUser { get; set; }
        public virtual UserProfile ModifiedByUser { get; set; }

        public virtual ICollection<UploadedCOESDetails> AuditZoneUploadedCOESDetails { get; set; }
        public virtual ICollection<Postcodes> AuditZonePostcode { get; set; }
    }

我需要做的是有人获取关键属性和表名..

目前我得到了以下代码

   TableAttribute tableAttr = dbEntry.Entity.GetType().GetCustomAttributes(typeof(TableAttribute), true).SingleOrDefault() as TableAttribute;

            // Get table name (if it has a Table attribute, use that, otherwise get the pluralized name)
            string tableName = tableAttr != null ? tableAttr.Name : dbEntry.Entity.GetType().Name;

            // Get primary key value (If you have more than one key column, this will need to be adjusted)
            var keyNames = dbEntry.Entity.GetType().GetProperties().Where(p => p.GetCustomAttributes(typeof(KeyAttribute), false).Count() > 0).ToList();

            string keyName = keyNames[0].Name;

如果我在模型中添加数据注释 [Key] 和 [Table],则此方法有效。但我已经在配置中定义了它们,所以我试图不重复工作......

有什么想法吗?


None

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

EntityTypeConfiguration如何获取关键属性? 的相关文章

随机推荐

  • 错误验证 W3C HTML5 img noscript facebook.com

    我正在尝试将我的页面验证为 HTML 5 W3c 我有以下错误 此处不允许使用 img 元素 作为 noscript 元素的子元素 noscript 元素作为 head 的子元素时 必须仅包含以下子元素 link style 和 meta
  • 在 Windows 上为 django 开发设置 virtualenv,

    当我尝试使用安装 MySQL python 时 第一次设置 virtualenv pip E lt
  • 为什么guard是基于Alternative的?

    Why is guard基于Alternative guard Alternative f gt Bool gt f guard b is pure if b is True and empty if b is False 我问是因为gua
  • 如何在matlab中制作三对角矩阵?

    如何从向量 1 2 3 4 5 创建下面的矩阵 1 0 0 0 0 2 1 0 0 0 3 2 1 0 0 4 3 2 1 0 5 4 3 2 1 0 5 4 3 2 0 0 5 4 3 0 0 0 5 4 0 0 0 0 5 这不是三对角
  • 是否可以使用 JQuery 或 JavaScript 更改窗口大小?

    我将创建 5 个按钮 例子 Button1 s value 640 480 Button2 s value 1024 600 Button3 s value 1600 900 Button4 s value 800 600 如果用户单击按钮
  • 通过 kinesis 触发跨账户 lambda

    我试图通过帐户 A 中的 Kinesis 流触发帐户 B 中的 lambda 这与描述的类似here https aws amazon com blogs compute easy authorization of aws lambda f
  • 连接属于表格行的 CSS 元素之间的垂直线

    我想用一条垂直线连接一些 CSS 圆圈 我尝试使用伪元素 after选择器如下 circle height 45px width 45px border radius 50 border 2px solid position relativ
  • 在 Unity 中使用捏合手势缩放滚动视图内容的正确方法是什么?

    目标 制作一个通用组件 支持捏合缩放设备和滚动鼠标缩放编辑器 捏合时 将枢轴设置为两个手指的中点 这样它就会围绕您捏合的位置缩放 我在下面提供了我的脚本 然而 当它 跳跃 或抖动很多时 它在两根手指捏合的设备上的滚动视图中效果不佳 不过 它
  • 如何在不重置服务器实例的情况下重置 application.cfc? [复制]

    这个问题在这里已经有答案了 可能的重复 重新启动应用程序而不重新启动服务器 https stackoverflow com questions 3119119 restart application without restarting s
  • 在弹出窗口中设置标题

    是否可以在弹出窗口中设置标题 我在 JavaScript 中有这个 var popup window open popup document title my title 但这不起作用 仍然看不到任何标题 编辑 页面弹出窗口显示的是 asp
  • 如何通过 Java 使用 Selenium Webdriver 从下拉列表中提取第一个选定选项的文本

    从下拉列表中选择一个选项后 我正在尝试在控制台中显示该选项 下面是我的代码 但我得到 ChromeDriver chrome on WINDOWS d5a01776981da5dacfeb89dbbc2e6b52 gt xpath name
  • std::map 是否自动平衡自身

    我知道STL映射 集的主流实现使用黑红树 我的问题是 这些实现在插入 删除元素时是否也会自动平衡树 如果没有 那么当元素排序和插入时 它总是追加到最右边的位置 最差的查找成本是 O n 那么 黑红树会自动平衡吗 是的 红黑树执行节点旋转以确
  • git fetch origin 不获取所有分支

    我读到的答案这个问题 https stackoverflow com questions 10312521 how to fetch all git branches that git fetch origin应该获取all起源的分支 但就
  • Azure WebApp 未加载我的字体

    我在 Azure 上的 WebApp 遇到了一些问题 我的自定义字体在我的本地主机上正确呈现 但是当我将项目部署到 Azure 并访问网站时 Web 浏览器通知我找不到我的字体 错误 404 但是当我从 Azure 门户访问 FTP 服务器
  • 使用 Python unittest 缓存 setUp() 的结果

    我目前有一个unittest TestCase 看起来像 class test appletrailer unittest TestCase def setup self self all trailers Trailers res 720
  • RandomForestRegressor 和 feature_importances_ 错误

    我正在努力从我的 RandomForestRegressor 中提取特征重要性 我得到 属性错误 GridSearchCV 对象没有属性 功能重要性 有谁知道为什么没有属性吗 根据文档应该存在这个属性 完整代码 from sklearn e
  • 如何释放堆栈上的变量?

    有没有办法释放在堆栈上创建的变量和 或对象 我特意谈论的是堆栈而不是堆 我不想争论这是否有用或良好的做法 我只需要知道它是否可能 我知道当它超出范围时它会自动释放 我想在它超出范围之前取消分配它 我正在使用 C int a a deallo
  • 在 Protractor 中测试模板?

    编写适用于站点中每个页面的断言的最佳方法是什么 我正在测试网站的页脚中是否存在某个元素 因此该元素应该存在于所有页面上 我正在考虑编写一个单独的文件来测试网站的模板元素 然后将其包含在所有规范中 不过似乎没有其他人这样做 首先 为了编写更干
  • Zend Framework:未定义的类常量'MYSQL_ATTR_INIT_COMMAND'

    您可能已经知道 我从之前的问题中从 ubuntu 切换到了 windows 我之前在 ubuntu 上开发 Zend Framework 现在在 Windows 上开发同一个项目 由于这种切换 我在 Windows 中遇到了一些在 ubun
  • EntityTypeConfiguration如何获取关键属性?

    我正在使用 C EF 5 和 MVC4 我正在使用 EntityTypeConfiguration 设置主键和后端表 public class AuditZoneMap EntityTypeConfiguration