2017-07-31 86 views

回答

1

添加属性partial类。然后得到一个类型列表与当前装配

Assembly thisAsm = Assembly.GetExecutingAssembly(); 
var tableTypes = thisAsm.GetTypes() 
    .Where(t => t.IsDefined(typeof(MyAttribute), false)); 
+0

谢谢你向我展示IsDefined方法,我直接使用DbContext类来处理它。 – Alex

0

好这个属性,我觉得我得到它的工作,而无需使用大会:

public class FoodAttribute : Attribute { } 
public class Fruit { } 
public class Coin { } 
public class Cereal { } 

public class FooContext : DbContext { 
    [Food] 
    public virtual DbSet<Fruit> Fruits { get; set; } 
    public virtual DbSet<Coin> Coins { get; set; } 
    [Food] 
    public virtual DbSet<Cereal> Cereals { get; set; } 
} 

要获得的属性食物表的列表:

var tableList = typeof(FooContext).GetProperties() 
    .Where(n => n.IsDefined(typeof(ClientTraitAttribute))) 
    .Select(n => n.PropertyType.GetGenericArguments()[0].Name).ToList(); 

感谢Yuriy向我展示IsDefined方法! :D

+0

DbContext文件不是自动生成的吗?除非你手动做。 –

+0

对,我从数据库(即数据库优先)开始执行Code First,因此这些文件只生成一次。 – Alex