One-To-Many relationship problem

One-To-Many relationship problem

Postby JoeRuspante » Thu 02 Aug 2012 22:01

Hi everybody.
I'm working with dotConnect 7 (last version downloaded last week).

I have this two model classes:

Code: Select all

public class User
{

    public User()
    {
       Settings = new List<UserSettings>();
    }

    public int Id { get; set; }

    public string Name { get; set; }

    public ICollection<UserSettings> Settings { get; set; }
}


public class UserSettings
{

     public int Id { get; set; }

     ... (other properties)

     public int UserId { get; set; }

     public User User { get; set; }
}




Working with DTO let me insert a new record on the UserSettings table when I know only the "Id" of the user.
But, if I use the convention (User property has UserId foreign key, as Microsoft explain), when I try to read data from the DB, it says that "UserId" column is not found.

If I take off the "UserId" property (as seen in devart example), how can I do the same insert but without read the "User" record?


I'd like to write something like that:

Code: Select all

UserSettings us = new UserSettings();
// I know there is a user with Id 1, but I won't read it from the DB
us.UserId = 1;
....
DbContext.Set<UserSettings>().Add(us);
DbContext.SaveChanges();





Can you tell me how to do?
Thank you in advance
JoeRuspante
 
Posts: 54
Joined: Mon 05 Jul 2010 23:08

Re: One-To-Many relationship problem

Postby Shalex » Tue 14 Aug 2012 13:00

Please try the code below. It works correctly in our environment with dotConnect for Oracle v 7.1.58 and EntityFramework.dll v 4.3.1.
Code: Select all
    class Program {
        static void Main(string[] args) {

            new OracleMonitor() { IsActive = true };

            var config = OracleEntityProviderConfig.Instance;
            config.Workarounds.IgnoreSchemaName = true;

            System.Data.Entity.Database.SetInitializer<MyDbContext>(new DropCreateDatabaseAlways<MyDbContext>());

            DbConnection con = new Devart.Data.Oracle.OracleConnection(
    "Data Source=orcl1120;User Id=Shalex2;Password=Shalex2;");

            using (MyDbContext context = new MyDbContext(con)) {
                var user = new User() { // User has Id == 1
                    Name = "New user"
                };
                context.Users.Add(user);
                context.SaveChanges();
            }

            using (MyDbContext context = new MyDbContext(con)) {
               //(context as IObjectContextAdapter).ObjectContext.CreateDatabase();

                UserSettings us = new UserSettings();
                // I know there is a user with Id 1, but I won't read it from the DB
                us.UserId = 1;
                context.Set<UserSettings>().Add(us);
                context.SaveChanges();
            }
        }
    }

    public class MyDbContext : DbContext {
        public MyDbContext(DbConnection connection)
            : base(connection, false) {
        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder) {
            modelBuilder.Conventions.Remove<System.Data.Entity.ModelConfiguration.Conventions.ColumnTypeCasingConvention>();
            modelBuilder.Conventions.Remove<IncludeMetadataConvention>();
        }

        public DbSet<User> Users { get; set; }
        public DbSet<UserSettings> UserSettings { get; set; }
    }
   
    public class User
    {
        public User()
        {
           Settings = new List<UserSettings>();
        }
        public int Id { get; set; }
        public string Name { get; set; }
        public ICollection<UserSettings> Settings { get; set; }
    }

    public class UserSettings
    {
         public int Id { get; set; }
         public int UserId { get; set; }
         public User User { get; set; }
    }
Shalex
Devart Team
 
Posts: 7705
Joined: Thu 14 Aug 2008 12:44


Return to dotConnect for Oracle