代码改变世界

Entity Framework练习题

2012-10-09 15:16  JustRun  阅读(1003)  评论(1编辑  收藏  举报

1. 问题: 变量id2的值是1,还是2? 假如如下面的注释操作,movies2有几条记录?里面会包含ID = 1的记录吗?

24073653

 

var id1 = db.Movies.First().ID;

var movies1 = db.Movies.ToList();

var movie = from m in db.Movies

where m.ID.Equals(1)

select m;

movie.First().ID = 2;

var id2 = db.Movies.First().ID;

//下面设置断点,运行到断点处后,手动向数据库中再添加一条记录

var movies2 = db.Movies.ToList();

 

2. 问题: movie2的price是多少?

 
 24073653
 
var movie1 = db.Movies.Find(1);
movie1.Price = 100;
var movie2 = db.Movies.Find(1);

 

3. 下面movie1和move2的类型一样吗?User1和User2类型一样吗?

定义了下面的Model

    public class Movie
    {
        public int ID { get; set; }
        public string Title { get; set; }
        public DateTime ReleaseDate { get; set; }
        public string Genre { get; set; }
        public decimal Price { get; set; }
        public virtual Category Category { get; set; }
    }

    public class Category
    {
        public int ID { get; set; }
        public string Name { get; set; }
        public virtual ICollection<Movie> Movies { get; set; }
    }
     
    public class User
    {
        public string Name{get;set;}
    }

 

使用不同的方式创建Movie, User对象

var movie1 = db.Movies.Create();
var movie2 = new Movie();

var user1 = db.User.Create();
var user2 = new User();