.Net Entity Framework Core Joining Multiple DB Contexts - entity-framework

I am trying to execute the below query
from st in _context.tbl_A
join ex in _context.tbl_B on st.ApplicantId equals ex.ApplicantId
join de in _contextCNS.tbl_C on st.DegreeId equals de.ID
where st.ApplicantId == AppId
On Executing the query I am getting ERRROR -------
System.ArgumentNullException: Value cannot be null.
Parameter name: entityType
at Microsoft.EntityFrameworkCore.Utilities.Check.NotNull[T](T value, String parameterName)
at Microsoft.EntityFrameworkCore.SqlServerMetadataExtensions.SqlServer(IEntityType entityType)
Thank you in advance

I notice that you are joining on a nullable columns please check this or this
Or other sources addressing "joining on a nullable"
Basically
you should specify a default value for the nullable
Or
Ignore the records having a value of null inside the where clause

Related

Entity Framework writing NULL to the DB

I am using EF v 6.2.0. My issue is similar to the SOF question - Entity Framework not claiming datetime.now is null. However, the proposed solution does not work for me.
buildDetail.BuildTime = epoch.AddMilliseconds(Double.Parse(buildTime)).ToLocalTime();
:
:
: // Other properties.
ps.dbEntities.BuildDetails.Add(buildDetailT);
ps.dbEntities.SaveChanges();
On the SaveChanges() line, I get an exception
Inner Exception 2:
SqlException: Cannot insert the value NULL into column 'BuildTime', table 'BuildDetailsDB.dbo.BuildDetails'; column does not allow nulls. INSERT fails.
The statement has been terminated.
The buildDetail.BuildTime property, when I hover over in debug, shows the value as {3/29/2018 9:17:22 AM} i.e. current date time. The datatype in Sql Server is datetime and the model I have generated from the database shows that this field has a return value of System.DateTime. So, I am frustrated as to why EF is trying to insert a null. In fact, if I allow nulls for this columns in the DB, that is exactly what it writes in that column - NULL.
The field in question is neither marked as Identity (both in model and db) nor Calculated. I have also updated the model from the Database and they are in sync.
Note: I have tried the DateTime.ParseExact and even an DateTime object converted from a string in the exact same format as expected in SQL Server i.e. "yyyy-MM-DD HH:MM:SS.FFF" with no luck and the same exception. eg.
buildDetailT.BuildTime = DateTime.Parse("2018-11-25 08:25:26.0000");

Spring Data Jpa equality of two column

I want to learn if we could write a query that has a condition like
List<Entity> findbyField1EqualsField2();
This method should not take any parameter . It should fetch entities which has a field1 equals field2. It is just a simple sql :
select * from entity where field1=field2.
But I could not find any solution yet. Thanks.
Create an operation with a query as next:
#Query("select t from entity t where t.field1 like t.field2")
List<T> findByField1LikeFie‌ld2();
I don't think findByField1LikeFie‌​ld2()works.... you would need to do it passing a param findByField1Like(St‌​ring param) and for this maybe you would need to load the entity before to get the value of field2.

entity framework working with entity properties that are null

In Entity framework, I am having entity properties which correspond to SQL Table columns which are ALLOW NULL = True in Database. When I use those properties in C#, if the value is null it gives exception. So how to handle this in EF, this happens for all the table columns that allow null.
I am assuming these columns are Nullable type in your .NET code, since they have ALLOW NULL = True in database. So, everytime you use these values, check for PropertyName.HasValue() which returns false whenever the property is null.

Something I don't quite understand about Conditional Mappings

In most cases, we can map a field in a table either to a property or we can map it using conditional mapping, but not both. Only exception is if condition is set to Is NotNull, since then we can also map to a column.
a) Is this the reason why we are able to map a DB column only once - namely, if field was allowed to have both a property mapping and a conditional mapping, then property mapping would tell EF to retrieve all table rows, while conditional mapping would tell EF to retrieve only those rows that satisfy the condition?!
b) If my reasoning under a) is correct, then why is field allowed to have both mappings when condition is set to Is NotNull? Why won't that create a conflict?
Thank you
Mapping with condition Is NotNull has special meaning because it requires subsequent change in your model. The mapped property in the model must not be nullable. So your column in the database is nullable, your mapping condition filters all records with null value and your property always receives only records with non-null values. Also you can never assign null to the property.
In case of common condition with value equality this special behaviour is not possible.

Doubt regarding JPA namedquery

I am trying to execute a namedquery
#NamedQuery(name="getEmployeeDetails",query="select e.username,e.email,e.image,e.firstname,e.lastname from Employee e where e.empid=?1")
Now when I execute this query in a EJB 3.0 Session Bean what is the object I should return.I tried returning Listits returning a Vector which creates a classcast exception.The employee table contains fields like password and other confidential details which I don't want to fetch.So I am not using select e from Employee e.
I am learning JPA can anyone help.
Below is the sample query which fetches only the required fields, but have to make such constructor for it.
Query : SELECT NEW package_name.Employee(e.username,e.email,e.image,e.firstname,e.lastname) FROM Employee e where e.empid=?1;
It will return Employee entity with selected fields & remaining will have default values.
Inspect the returned type by calling .getClass() on a returned object. I'd guess it's an array.
But this is not really a good way to use JPA. Select the whole entity and then just don't use what you don't need. It's not such a performance hit.