Entity Framework 6.0 supports enumeration/the primary key is reversed after the Entity is added. entityframework

Source: Internet
Author: User
Tags connectionstrings

Entity Framework 6.0 supports enumeration/the primary key is reversed after the Entity is added. entityframework
Lab

Go directly to the code and view the result

Entity class

[Flags]    public enum FlagsEnum    {        Day = 1,        Night = 2    }    public class EntityWithEnum    {        public int ID { get; set; }        public FlagsEnum ValidTime { get; set; }    }

 

Database Context

public partial class CodeFirstModel : DbContext   {        public CodeFirstModel()            : base("name=CodeFirstModel")        {        }        public virtual DbSet<EntityWithEnum> EntityWithEnum { get; set; }}

SQL server LocalDb connection string

<connectionStrings>    <add name="CodeFirstModel" connectionString="data source=(LocalDb)\MSSQLLocalDb;initial catalog=TestDb;integrated security=True;multipleactiveresultsets=True;application name=EntityFramework" providerName="System.Data.SqlClient" />  </connectionStrings>

 

Main Function

 

static void Main(string[] args)        {            //CreateDatabaseIfNotExists            //DropCreateDatabaseIfModelChanges            //DropCreateDatabaseAlways            Database.SetInitializer(new DropCreateDatabaseIfModelChanges<CodeFirstModel>());            using (var context = new CodeFirstModel())            {                context.Database.Initialize(true);            }            using(CodeFirstModel db = new CodeFirstModel())            {        EntityWithEnum entity = new EntityWithEnum() { ValidTime = FlagsEnum.Day };                db.EntityWithEnum.Add(entity);                entity = new EntityWithEnum() { ValidTime = FlagsEnum.Night };                db.EntityWithEnum.Add(entity);                entity = new EntityWithEnum() { ValidTime = FlagsEnum.Night| FlagsEnum.Day };                db.EntityWithEnum.Add(entity);                db.SaveChanges();                Console.WriteLine("{0}:\t{1}", entity.ID, entity.ValidTime);                Console.WriteLine("-------------------");                var entitys = from e in db.EntityWithEnum                              where e.ValidTime.HasFlag(FlagsEnum.Day)                              select e;                foreach (var item in entitys)                {                    Console.WriteLine("{0}:\t{1}", item.ID, item.ValidTime);                }                            }            Console.ReadKey(true);}

Running result

 

Conclusion

The LINQ of Entity Framework 6.0 can directly write the HasFlags method of enumeration.

After the added object is saved in the database, the primary key generated by the database is read out. You do not need to write the object to the database and check it again.


How does entity framework set a composite primary key?

Multiple Primary keys are allowed, as shown below:

Using System; using System. componentModel. dataAnnotations; using System. componentModel. dataAnnotations. schema; public class Entity {[Key, Column (Order = 0)] public int Key1 {get; set;} [Key, Column (Order = 1)] public string Key2 {get; set;} [Key, Column (Order = 2)] public string Key3 {get; set ;}//....}

Entity Framework non-primary key Association questions, please come to answer !!!

Var query = from t in Category
Join p in Product
On p. CategoryNo equals t. No
Select new Category {
// Assign values to attributes of Category, for example:
// ID = t. ID}
List <Category> listType = query. ToList ()
 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.