如何防止实体模型中的类型和属性保留到数据库?

时间:2014-09-24 14:36:34

标签: c# sql entity-framework

我创建了一个使用实体框架来创建数据库的MVC项目。我有2个模型,每个模型有大约5个字段。但是,我希望其中一些字段具有我自己创建的唯一返回类型,但我不希望将唯一的数据类型制作成表格。

修改

根据评论:为了澄清,我仍然希望人员和职业表在适当的部分下有孩子和地址数据

例如:

型号:

class Person {
 [Key]
 public int PersonID{get;set}
 public string name {get;set;}
 public int age {get;set;}
 //note below: return type is ICollection of type Kids
 //or it can just be a return type of type Kids like so
 //public Kids kids {get;set;}
 public ICollection<Kids> kids {get;set;}
 public int OccupationID{get;set;}
 public virtual Occupation Occupation {get;set}
}

class Occupation {
 [Key]
 public int OccupationID{get;set;}
 public string OccupationName{get;set}
 //note the line below has a return type of Address
 public Address Location{get;set;}
 public int PersonID{get;set;}
 public virtual Person Person{get;set;}

}

上面使用的其他数据类型

class Kids {
 public int numberOfKids{get;set;}
 public List<string> namesOfKids{get;set;}
}

class Address {
 public string street{get;set;}
 public string city{get;set;}
 public int apartmentNumber{get;set;}
}

如果我不想将KidsAddress数据类型放入数据库表中,那么这些数据类型将放在项目中?我问这个是因为我尝试创建一个名为&#34; src&#34;的新文件夹。并将它们放在该文件夹中,但是当我尝试为Person模型创建一个新的脚手架项时,我收到一条错误消息,说这些数据类型没有密钥。

另一个问题:我知道我不知道如何不将这些数据类型转换成表格,但这是一种不好的做法吗?我也应该把它们变成桌子吗?

另外:我可以拥有多个虚拟字段吗?或者这会破坏什么?

我非常感谢帮助

3 个答案:

答案 0 :(得分:2)

如果你想要实现的只是在数据库中没有创建某些类字段,那么你可以使用[NotMapped]数据注释

class Occupation {
    [Key]
    public int OccupationID{get;set;}
    public string OccupationName{get;set}

    [NotMapped]
    public Address Location{get;set;}
    public int PersonID{get;set;}
    public virtual Person Person{get;set;}
}

在上面的场景中,在创建数据库结构时,以及查询数据库时,[NotMapped]属性下的字段将被忽略。

可在此处详细了解该课程:http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.schema.notmappedattribute(v=vs.110).aspx

除此之外,不要在DbContext中包含任何您不希望为其创建表的类

此外,就can I have multiple virtual fields? or does that break something?而言:

实体框架中的

virtual属性主要用于允许延迟加载(如果已在系统中配置)。

但它也会对更改跟踪产生影响。如果您正在使用纯POCO或使用代理但不要将属性设置为虚拟,则更改检测变得更加复杂。您可以在此处详细了解相关内容:http://blog.oneunicorn.com/2012/03/10/secrets-of-detectchanges-part-1-what-does-detectchanges-do/

答案 1 :(得分:1)

如果您不想要映射类,请不要将其作为DbContext包含在DbSet<T>类中。

如果在类中,你不想要映射属性,你可以在属性中使用NotMapped属性,或者,如果你使用流畅的api,你可以做这样的事情:

modelBuilder.Entity<Person>().Ignore(p => p.LastName);

答案 2 :(得分:0)

一种方法是使用一个接口来按照你想要的方式塑造类,并在getter中组合复杂的对象:

public interface IPerson
{
    string name {get;set;}
    int age {get;set;}
    Kids kids {get;}
    Occupation Occupation {get;}
    void AddKid(string name);
}

public class Person : IPerson 
{
    [Key]
    public int PersonID{get;set}
    public string name {get;set;}
    public int age {get;set;}
    [NotMapped]
    public Kids kids { get
        {
            Kids k = new Kids
            {
                namesOfKids = (new [] 
                {
                    Name1,
                    Name2,
                    Name3,
                    // ...
                }).Where(n => n != null).ToList();
            };
            k.numberOfKids = k.namesOfKids.Count;
            return k;
        }
    }

    public int numberOfKids{get; set;}
    public List<string> namesOfKids{get; set;}

    public int OccupationID{get;set;}
    [NotMapped]
    public Occupation Occupation {get { // TODO }}
    public string Name1 { get; set; }
    public string Name2 { get; set; }
    // ... Add more name fields up to the max

    public void AddKid(string name)
    {
        switch (numberOfKids)
        {
            case 0: Name1 = name;
                    break;
            case 1: Name2 = name;
                    break;
            // ...

        }
        numberOfKids ++;
    }
}

class Kids 
{
    public int numberOfKids{get;set;}
    public List<string> namesOfKids{get;set;}
}

需要注意的是,您只能在代码中使用IPerson个对象。您可以将任何Person对象直接投射到IPerson

这不是编译或测试过的,但它应该提供如何实现目标的想法。