使用实体代码第一种方法找不到我的数据库 [英] Can't find my Database using Entity Code First Approach

查看:86
本文介绍了使用实体代码第一种方法找不到我的数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 

我刚开始使用Entity Framework,我正在使用代码优先方法。在我的模型中,我有一个产品和产品类别。产品属于类别,类别可以有几种产品。

 

I'm just getting started with Entity Framework, am using the code first approach. In my Models I have a Product and ProductCategories. A product belongs to a categpry and a Category can hav several products.

我的型号是 如下所示:

My Models are  as below:

 public class Product
    {
       
            //Model properties --> Table Name in SQL
            [Key]
            public int ProductId { get; set; }
            public string ProductName { get; set; }
            public int CategoryId { get; set; }
            //Foreign Key -----> To establish relationship btw two tables.
            //FK points to the Primary Key of the table it's refrencing


            // A product belongs to a ----> Category
            public virtual ProductCategory Category { get; set; }

            /*Virtual --> Navigation property.It enables the Lazy Loading feature of EF.
            Lazy --> The content of the property with the "virtual" will be automatically loaded from the Db
            when try to access them.*/
     
    }










public class ProductCategory
    {
            //Model properties --> Table Name in SQL
            [Key] //PK
            public int CategoryId { get; set; }
            public string CategoryName { get; set; }
            // A Category ----> has many products (List of Products)
            public virtual List<Product> Products { get; set; }

            /*Virtual --> Navigation property.It enables the Lazy Loading feature of EF.
            Lazy --> The content of the property with the "virtual" will be automatically loaded from the Db
            when try to access them.*/
     
    }




我的上下文类




class Context: DbContext
    {
        /*make context clas inherit the DbContext System.Data.Entity.DbContext
    and exposes a typed DbSet<TEntity> for each class in our model.*/
        
            public DbSet<Product> products { get; set; }
            public DbSet<ProductCategory> categories { get; set; }

            //products, categories --> are used to access this Entites (Tables) to perform CRUD operations from our GenericDAL

        }







    static void Main(string[] args)
        {
            //instantiate GenericDAL to be able to access our data base and perform CRUD Operation on its Entities (Tables)
            GenericDAL<Product> objPro = new GenericDAL<Product>();
            GenericDAL<ProductCategory> objProCat = new GenericDAL<ProductCategory>();

            Product pro = new Product { ProductName = "Leisure" };
            objPro.Insert(pro);

            Context db = new Context();

           
            foreach ( var p in db.products) {
            Console.WriteLine(p.ProductName);
            }
            Console.ReadLine();

        }

打印的应用程序:

休闲休闲休闲郎

Leisure Leisure Leisure




但是当我打开我的SQL Server管理器时,我找不到数据库而且我没有收到任何错误。我做错了什么,任何人都可以帮帮忙....谢谢你b $ b

But When I open my SQL Server Manager I can't find the Database and I don't get any error. What am I doing wrong, Could any one help out .... Thanks




推荐答案

嗨!

这些链接非常有用:

  • https://stackoverflow.com/questions/27983083/default-physical-location-of-data-base-created-in-entity-framework-codefirst-app
  • https://stackoverflow.com/questions/8003588/entity-code-first-where-is-my-db-file-stored
  • https://msdn.microsoft.com/en-us/library/jj200620(v=vs.113).aspx


希望它有用。

Hope it wil be useful.


请将
记得将答复标记为答案,如果它们有帮助,这将有助于其他正在寻找解决相同或类似问题的人
。 

Please remember to mark the replies as answers if they help, this will help others who are looking for solutions to the same or similar problem


在Twitter上关注我:  https://twitter.com/NordineMhoumadi


这篇关于使用实体代码第一种方法找不到我的数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆