Pass in the proper connection string to UseSqlServer.
I even tried to hard-coded the connection strings, but still got SAME exception.
You know it's solved, but you refuse to use the solution?
I tried this "solution", but still got SAME exception.
Let me change an angle to state my question. I want to get the current username while inserting a record into the 2nd database. My following code throws exception:
public async Task CreateObj(ObjListDto input) { var obj = ObjectMapper.Map<Obj>(input); obj.Username = GetCurrentUser().UserName; //this line caused the exception await _objRepository.InsertAsync(obj); }
Can you give me another way to get the current username ?
Thanks,
Yes. My both DbContexts work fine, if use them separately. For example, I can get "accessToken" from /api/TokenAuth/Authenticate. The user entity is in ProjectDbContext. I can get "myEntity" from CustomDbContext.
There seems no built-in way to add multiple DbContexts for multiple databases so far. I tried the way your indicated, but it does not work for me. My CustomDbContext never connected in that way. I know I must miss something. I am still waiting for an "official" release for using multiple DbContext.
Currently, I use my own workaround. I made an "AppEntityFrameworkCoreModule" which is very similar to "ProjectEntityFrameworkCoreModule", but with different connection string. I added "typeof(App1EntityFrameworkCoreModule)" on the top of WebCoreModule, so both DbContexts can be initialized. They work fine until the issue I met today.
Here is my problem code for inserting a record into the table in the 2nd database. The "Obj" is an entity in the 2nd DbContext.
public async Task CreateObj(ObjListDto input)
{
var obj = ObjectMapper.Map<Obj>(input);
obj.Username = GetCurrentUser().UserName; //this line caused the exception
await _objRepository.InsertAsync(obj);
}
Any idea?
Just added this namespace, and passed compile, but unfortunately, it still throws the same exception.
Any idea?
Thank you for your prompt response!
I am trying to add that line into PreInitialize method of EntityFrameworkCoreModule, with 2 namespaces: using Abp.EntityFrameWorkCore.Uow; using Abp.Dependency;
but got a compile error: The non-generic method 'IAbpStartupConfiguration.ReplaceService(Type, Action)' cannot be used with type arguments.
What did I miss?
I am using (core+angular) v4.5.1, and need to add a service for creating a record into database table. The service works fine until I insert the current username into the record.
I am using two database with separated DbContext. I need to get the username by GetCurrentUser().UserName from DbContext1 and insert the username into an entity in DbContext2. It seems not allowed this operation, and throw out an exception:
Mvc.ExceptionHandling.AbpExceptionFilter - The specified transaction is not associated with the current connection. Only transactions associated with the current connection may be used.
I searched it on forum and found a similar issue on "2 Context - Transaction Error #372". It seems saying the issue solved on "the milestones: v4.4, v4.5 on Aug 25 ". I wonder if my above issue is new? If so, can you give a clue for a workaround?
Thanks,
Excellent!!
I will try this way later when I handle complicated mapping case.
Thanks again!!
Nice and Neat! Thank you very much!!
However, I got two glitches:
I actually want to entity1 LEFT JOIN entity2. Your code seems just working as "inner Join". When I try your code with an empty entity2, it results an empty EntityDto. It should be an EntityDto with values of all PropertyFromEntity1 but null for all PropertyFromEntity2. I reversed the order of mapping and solved it. Is it the correct way?
Another problem is about Id. As convention, both entity1 and entity2 have field "Id". I want to keep entity1.Id as EntityDto.Id and ignore entity2.Id. When I try your code, it returns the EntityDto.Id = entity2.Id, because the last mapping is on the entity2, I think. I surely can reverse the order of mapping in this particular example to solve this issue, but generally, how do I handle if entity1 and entity2 have same name properties?
For example, if I need:
[AutoMapFrom(typeof(Entity1), typeof(Entity2))] public class EntityListDto : EntityDto { public string XYZ { get; set; } // map with Entity1.XYZ, instead of Entity2.XYZ public string ABC { get; set; } // map with Entity2.ABC, instead of Entity1.ABC }
Usually, most of properties can be auto-mapped correctly, but only few of them (such as Id, name, email ...) have this kind of issue when I map joined entities. Can you show me a correct way?
Thanks again!
I need to output a list (EntityListDto) to client. This EntityListDto is mapped from two entities:
[AutoMapFrom(typeof(Entity1), typeof(Entity2))] public class EntityListDto : EntityDto { public string PropertyFromEntity1 { get; set; } public string PropertyFromEntity2 { get; set; } }
I made a service:
public EntityListDto GetEntity(int Id)
{
var entity1 = _entityt1Repository
.GetAll()
.Where(w => w.Id == Id);
var entity2 = _entity2Repository
.GetAll()
.Where(w => w.FKId == Id);
var entity = entity1.GroupJoin(entity2, e1 => e1.Id, e2 => e2.FkId, (e1, e2) => new { entity1 = e1, entity2 = e2.FirstOrDefault() }).FirstOrDefault();
return ObjectMapper.Map<EntityListDto>(entity);
}
It throws an exception on the last line:
AutoMapper.AutoMapperMappingException: 'Missing type map configuration or unsupported mapping.'
How should I use ObjectMapper to Map the joined entity?
Thanks,
.Net Framework 4.6.1.
BTW, Our SMTP on IIS is shared by several applications, so I have no too much freedom to adjust its settings. I think it is a popular scenario. Is it possible for ASPNET Zero to inherit all settings from the SMTP on host? For example, on Administration>Settings>Email (SMTP) page, if any setting value keeps blank, use the value on SMTP host by default.
Thank you!
I just updated it to ngx-bootstrap v1.9.3, and solved all of the issues. :o