Base solution for your next web application
Open Closed

Is it possible to use ADO.NET Entity Data Model #4468


User avatar
0
drblakl created

Hello!,

I know that asp.net zero primarily focuses around code first migrations.

Is it at all possible to use a Database first ADO.NET Entity Data Model within asp.net zero?

if so, how would one go about making this happen?

Thank you!


5 Answer(s)
  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi @drblakl,

    If you want to use repository pattern for data access, then you can first disable code first migrations on your project and then you need to create your new entities from db tables using a third party tool like this one <a class="postlink" href="https://marketplace.visualstudio.com/items?itemName=SimonHughes.EntityFrameworkReversePOCOGenerator">https://marketplace.visualstudio.com/it ... OGenerator</a>.

    Probably there are some other steps because we haven't tried DB first approach on AspNet Zero but if you want to try this and if you face any problems, we can try to help.

    By the way, we don't suggest DB first approach for AspNet Zero.

  • User Avatar
    0
    bbakermmc created

    What we do is make a new context for all our dbfirst models and such. This way the context AspNetZero uses isnt touched. Not sure what youre trying to do persay but this works for us and it doesnt impact what AspNetZero does or what we do.

  • User Avatar
    0
    drblakl created

    The ideal scenario would be:

    Use the ADO.NET Visual Editor / Designer to access tables. This would allow us to easily track changes within the database simply by updating the model from the database.

    If we created a new repository would we be able to use Multitenancy and features that are available by using Asp.Net Zero?

    Could we ensure multitenancy? If so, how so?

    Would we have the same control over these entities we would as if we had created them using the Code First Method?

    That's the main thing, I don't want to use the ADO.NET Entity Data Model and lose mass amounts of functionality.

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi @drblakl,

    These are two different approaches (DB First and Code First). AspNet Zero comes with Code first approach and doesn't support DB First directly. This can be done but we don't offer DB first approach with AspNet Zero.

    Since we haven't tried DB First approach with AspNet Zero, it will not be fair to give you any suggestions. So, it is better for you to use code first approach with AspNet Zero.

  • User Avatar
    0
    bbakermmc created

    We use DBFirst with our multi tenant setup. Like I said you just need to make a 2nd context thats going to be your DBFirst models. We generate all the POCOs separately then we just add them to this new context. Everything then just "works" with the default aspnetzero repos etc.

    Note: we do multi tenancy per db not 1 table for all tenants. So every DB has their own tables.

    public class DMPContextFactory : IDesignTimeDbContextFactory<DMPContext>
        {
            public DMPContext CreateDbContext(string[] args)
    
            {
                var builder = new DbContextOptionsBuilder<DMPContext>();
                var configuration = AppConfigurations.Get(WebContentDirectoryFinder.CalculateContentRootFolder());
    
                DMPContextConfigurer.Configure(builder, configuration.GetConnectionString(PlatformConsts.ConnectionStringName));
                
                return new DMPContext(builder.Options);
            }
        }
    
    public static class DMPContextConfigurer
        {
            public static void Configure(DbContextOptionsBuilder<DMPContext> builder, string connectionString)
            {
                builder.UseSqlServer(connectionString, o => o.UseRowNumberForPaging());
            }
    
            public static void Configure(DbContextOptionsBuilder<DMPContext> builder, DbConnection connection)
            {
                builder.UseSqlServer(connection, o => o.UseRowNumberForPaging());
            }
        }
    
    Configuration.Modules.AbpEfCore().AddDbContext<DMPContext>(options =>
                    {
                        //DMPContextConfigurer.Configure(configuration.DbContextOptions, configuration.ConnectionString);
    
                        if (options.ExistingConnection != null)
                        {
                            DMPContextConfigurer.Configure(options.DbContextOptions, options.ExistingConnection);
    
                        }
                        else
                        {
                            DMPContextConfigurer.Configure(options.DbContextOptions, options.ConnectionString);
                        }
    
                    });
    
    public class DMPContext : AbpDbContext
        {
            public DMPContext(DbContextOptions<DMPContext> options)
                : base(options)
            {
            }
    
            public DbSet<RpilistenerQueue> RpilistenerQueue { get; set; }
            public DbSet<Brc> Brc { get; set; }
            public DbSet<Brcquestion> Brcquestion { get; set; }
            public DbSet<Brctype> Brctype { get; set; }
            public DbSet<Package> Package { get; set; }
            public DbSet<Question> Question { get; set; }
            public DbSet<Source> Source { get; set; }
            public DbSet<LookupValue> LookupValue { get; set; }
            public DbSet<Answer> Answer { get; set; }
            public DbSet<ImportTwilioMessageQueue> ImportTwilioMessageQueue { get; set; }
            public DbSet<Contact> Contact { get; set; }
            public DbSet<ContactEvent> ContactEvent { get; set; }
            public DbSet<ContactEventActivity> ContactEventActivity { get; set; }
            public DbSet<AddressUnavailableReason> AddressUnavailableReason { get; set; }
            public DbSet<Channel> Channel { get; set; }
            public DbSet<Direction> Direction { get; set; }
            public DbSet<Campaign> Campaign { get; set; }
            public DbSet<ContactAddress> ContactAddress { get; set; }
            public DbSet<ContactIdentifierType> ContactIdentifierType { get; set; }
            public DbSet<ContactAnalytics> ContactAnalytics { get; set; }
            public DbSet<ContactAttributeName> ContactAttributeName { get; set; }
            public DbSet<ContactAttributeValue> ContactAttributeValue { get; set; }
            public DbSet<ContactInfo> ContactInfo { get; set; }
            public DbSet<ContactvDbs> ContactvDbs { get; set; }
            public DbSet<ContactMailOptStatus> ContactMailOptStatus { get; set; }
            public DbSet<ContactPhoneOptStatus> ContactPhoneOptStatus { get; set; }
            public DbSet<ContactEmailOptStatus> ContactEmailOptStatus { get; set; }
            public DbSet<ContactTextOptStatus> ContactTextOptStatus { get; set; }
            public DbSet<ContactFaxOptStatus> ContactFaxOptStatus { get; set; }
            public DbSet<CompanyContact> CompanyContact { get; set; }
            public DbSet<CompanyIdentifierType> CompanyIdentifierType { get; set; }
            public DbSet<CompanyAnalytics> CompanyAnalytics { get; set; }
            public DbSet<CompanyAttributeName> CompanyAttributeName { get; set; }
            public DbSet<CompanyAttributeValue> CompanyAttributeValue { get; set; }
            public DbSet<CompanyvDbs> CompanyvDbs { get; set; }
            public DbSet<CompanyInfo> CompanyInfo { get; set; }
            public DbSet<QuestionResponse> QuestionResponse { get; set; }
            public DbSet<ContactAddressType> ContactAddressType { get; set; }
            public DbSet<ContactPhone> ContactPhone { get; set; }
            public DbSet<ContactEmail> ContactEmail { get; set; }
            public DbSet<ContactIdentifier> ContactIdentifier { get; set; }
            public DbSet<ContactNote> ContactNote { get; set; }
            public DbSet<CompanyAddress> CompanyAddress { get; set; }
            public DbSet<CompanyAddressType> CompanyAddressType { get; set; }
            public DbSet<CompanyPhone> CompanyPhone { get; set; }
            public DbSet<CompanyEmail> CompanyEmail { get; set; }
            public DbSet<CompanyIdentifier> CompanyIdentifier { get; set; }
            public DbSet<CompanyNote> CompanyNote { get; set; }
            public DbSet<Company> Company { get; set; }
            public DbSet<TextTarget> TextTarget { get; set; }
            public DbSet<Country> Country { get; set; }
            public DbSet<TextMessage> TextMessage { get; set; }
            public DbSet<TextMessageHistory> TextMessageHistory { get; set; }
            public DbSet<TextMessageQueue> TextMessageQueue { get; set; }
    
            public DbSet<DataAugmentationCompanyAddress> DataAugmentationCompanyAddress { get; set; }
            public DbSet<DataAugmentationCompanyEmail> DataAugmentationCompanyEmail { get; set; }
            public DbSet<DataAugmentationCompanyPhone> DataAugmentationCompanyPhone { get; set; }
            public DbSet<DataAugmentationContactAddress> DataAugmentationContactAddress { get; set; }
            public DbSet<DataAugmentationContactEmail> DataAugmentationContactEmail { get; set; }
            public DbSet<DataAugmentationContactPhone> DataAugmentationContactPhone { get; set; }
            public DbSet<DataAugmentationContact> DataAugmentationContact { get; set; }
            public DbSet<DataAugmentationCompany> DataAugmentationCompany { get; set; }
            public DbSet<DataAugmentationListQuestion> DataAugmentationListQuestion { get; set; }
            public DbSet<DataAugmentationQuestion> DataAugmentationQuestion { get; set; }
            public DbSet<DataAugmentationResponse> DataAugmentationResponse { get; set; }
            public DbSet<DataAugmentationList> DataAugmentationList { get; set; }
            public DbSet<DataAugmentationQueue> DataAugmentationQueue { get; set; }
        }
    
    private readonly IRepository<CompanyAddress, int> _dmpCompanyAddressRepository;
            private readonly IRepository<CompanyContact, int> _dmpCompanyContactRepository;
            private readonly IRepository<CompanyEmail, int> _dmpCompanyEmailRepository;
            private readonly IRepository<CompanyPhone, int> _dmpCompanyPhoneRepository;
    
    var newRec = new DataAugmentationCompanyPhone
                {
                    CompanyPhoneId = companyPhoneId,
                    DataAugmentationSessionId = sessionId,
                    EditCount = 1,
                    Inserted = DateTime.Now,
                    Updated = DateTime.Now,
                    InsertedBy = _sessionAppService.GetCurrentLoginInformations().Result.User.UserName,
                    UpdatedBy = _sessionAppService.GetCurrentLoginInformations().Result.User.UserName,
                    VerificationDate = DateTime.Now
                };
    
                _daCompanyPhoneRepository.Insert(newRec);