Click here to Skip to main content
15,881,424 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hello everyone,

i need to help in Entity Framework. I have multiple ConnectionStrings example "DB_A" and "DB_B"
i have to try enrich the Data from DB_A tables into DB_B tables. How can i do this ?

What I have tried:

i use Connectionmanager in .net core
Posted
Updated 24-Sep-21 0:06am
Comments
Dave Kreskowiak 13-Aug-21 9:45am    
What on earth does "enrich" mean in the context of databases?
ugurarslanm 13-Aug-21 9:49am    
i mean "save"
PIEBALDconsult 13-Aug-21 10:18am    
"Copy" data from one to the other?
ugurarslanm 13-Aug-21 10:34am    
i have two DbContext and i use RabbitMQ. I Post the Data into DB_A Table and the RabbitMQ processed that as a Message. And this Message must Save to the DB_B. The Tables from A and B is the same.
ugurarslanm 13-Aug-21 10:35am    
i have to try work with 2 dbContext. How can I do this ?

1 solution

Maybe this helps:
I uses a ContextBase and multiple implementations (e.g. SqLite, InMemory, SQL Server).
I didn't check the copy method, but this should work or be a useful base:


using Microsoft.EntityFrameworkCore;
using System.Linq;

namespace Foo
{
    public class CopyData
    {
        public async void CopyAToB()
        {
            using var source = new FooSqliteContext("d:\\source.sqlite");
            using var target = new FooInMemoryContext();

            var sourceEntites = await source.Entites.ToArrayAsync();
            target.Entites.AddRange(sourceEntites);

            //DTO if needed...
            //var targetDto = sourceEntites.Select(sourceEntity => new FooContextBase.Entity() { Name = sourceEntity.Name });
            //target.Entites.AddRange(targetDto);

            target.SaveChanges();
        }
    }
    public abstract class FooContextBase : DbContext
    {
        public class Entity
        {
            public int Id { get; set; }
            public string Name { get; set; }
        }
        public DbSet<Entity> Entites { get; set; }
    }

    public class FooInMemoryContext : FooContextBase
    {
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        => optionsBuilder.UseInMemoryDatabase("InMemoryDb");
    }
    public class FooSqliteContext : FooContextBase
    {
        public string SqLiteDbPath { get; set; }

        public FooSqliteContext(string dbPathAndFile)
        {
            SqLiteDbPath = dbPathAndFile;
            Database.EnsureCreated();
        }
        protected override void OnConfiguring(DbContextOptionsBuilder options)
            => options.UseSqlite($"Data Source={SqLiteDbPath}");
    }

}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900