none
Does EF support Schema of SQL server RRS feed

  • Question

  • In SQL server we can create Schema's.  Does EF 5 onwards support Schema. How it works?

    Tuesday, April 21, 2015 9:55 AM

Answers

  • Hello Dotnetavalanche,

    >>Does EF 5 onwards support Schema. How it works?

    If you are using Code first approach, it supports to create different schema tables by by using the entity classes' attributes or through the DbModelBuilder API, here I created a demo you could check it:

    using System;
    
    using System.Collections.Generic;
    
    using System.ComponentModel.DataAnnotations.Schema;
    
    using System.Data.Entity;
    
    using System.Linq;
    
    using System.Text;
    
    using System.Threading.Tasks;
    
    
    namespace CFs
    
    {
    
        public class Person
    
        {
    
            public int Id { get; set; }
    
            public string Name { get; set; }
    
    
            public virtual HashSet<Message> Messages { get; set; }
    
    
            public int CountryId { get; set; }
    
            public virtual Country Country { get; set; }
    
        }
    
        public class Message
    
        {
    
            public int Id { get; set; }
    
            public string Name { get; set; }
    
            public string Text { get; set; }
    
    
            public int PersonId { get; set; }
    
            public Person Person { get; set; }
    
        }
    
        [Table("Country", Schema = "A")]
    
        public class Country
    
        {
    
            public int Id { get; set; }
    
            public string Name { get; set; }
    
        }
    
        internal class SC20150413 : DbContext
    
        {
    
            public DbSet<Person> Persons { get; set; }
    
            public DbSet<Country> Countries { get; set; }
    
            public DbSet<Message> Messages { get; set; }
    
            protected override void OnModelCreating(DbModelBuilder modelBuilder)
    
            {
    
                modelBuilder.Entity<Person>().ToTable("Person", schemaName: "B");
    
            }
    
        }
    
    
    }
    

    The Person will be in schema B, Country will be in schema A, and the Message would be in the default schema dbo.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Saturday, April 25, 2015 7:45 AM
    Moderator

All replies

  • Hello Dotnetavalanche,

    >>Does EF 5 onwards support Schema. How it works?

    If you are using Code first approach, it supports to create different schema tables by by using the entity classes' attributes or through the DbModelBuilder API, here I created a demo you could check it:

    using System;
    
    using System.Collections.Generic;
    
    using System.ComponentModel.DataAnnotations.Schema;
    
    using System.Data.Entity;
    
    using System.Linq;
    
    using System.Text;
    
    using System.Threading.Tasks;
    
    
    namespace CFs
    
    {
    
        public class Person
    
        {
    
            public int Id { get; set; }
    
            public string Name { get; set; }
    
    
            public virtual HashSet<Message> Messages { get; set; }
    
    
            public int CountryId { get; set; }
    
            public virtual Country Country { get; set; }
    
        }
    
        public class Message
    
        {
    
            public int Id { get; set; }
    
            public string Name { get; set; }
    
            public string Text { get; set; }
    
    
            public int PersonId { get; set; }
    
            public Person Person { get; set; }
    
        }
    
        [Table("Country", Schema = "A")]
    
        public class Country
    
        {
    
            public int Id { get; set; }
    
            public string Name { get; set; }
    
        }
    
        internal class SC20150413 : DbContext
    
        {
    
            public DbSet<Person> Persons { get; set; }
    
            public DbSet<Country> Countries { get; set; }
    
            public DbSet<Message> Messages { get; set; }
    
            protected override void OnModelCreating(DbModelBuilder modelBuilder)
    
            {
    
                modelBuilder.Entity<Person>().ToTable("Person", schemaName: "B");
    
            }
    
        }
    
    
    }
    

    The Person will be in schema B, Country will be in schema A, and the Message would be in the default schema dbo.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Saturday, April 25, 2015 7:45 AM
    Moderator
  • Thanks Fred!!
    Wednesday, September 9, 2015 4:36 PM