Column mapper in linq2db is not working the same as in EF Column mapping - linq-to-sql

This entity/Poco was working fine with EF:
[Column(Name="myFieldNameOnDB")]
public string MyPropNameOnClass{ get; set; }
After replacing EF in the entire solution with Linq2DB, it is working where poco class properties are the same as column/field name, but for some classes, there are some properties that are different than a column in name, that was working with specifying a different name in EF with System.ComponentModel.DataAnnotations but I wonder why not working with LinqToDB.Mapping instead of EF and annotation?
I got the below error:
System.Data.SqlClient.SqlError: Invalid column name myFieldNameOnDB
Any help?
Update:
My class refer to a SQL View

Related

Hibernate search get a row by a field value

I'm new to hibernate and Spring.
I have Student class that have:
studentId;
firstName;
lastName;
yearLevel;
getters/setters
...etc
I want to search a student by its firstName or lastName.
So I tried this method in StudentDaoImpl.java:
#Override
public Student getStudentByLastName(String lastName) {
return (Student)session.getIdentifier(lastName);
}
And it does not work the only one that works is searching by studentId:
public Student getStudentByLastName(int studentId) {
return (Student)sessionFactory.getCurrentSession().get(Student.class,studentId);
}
Is there a way to get the Student object by searching a field value like lastName or firstName ?
I think you shuld use camelCase in naming variables (firstName instead of firstname etc.).
http://docs.spring.io/spring/docs/3.0.x/spring-framework-reference/html/beans.html#beans-beanname
Bean naming conventions
The convention is to use the standard Java convention for instance
field names when naming beans. That is, bean names start with a
lowercase letter, and are camel-cased from then on. Examples of such
names would be (without quotes) 'accountManager', 'accountService',
'userDao', 'loginController', and so forth.
Naming beans consistently makes your configuration easier to read and
understand, and if you are using Spring AOP it helps a lot when
applying advice to a set of beans related by name.

Entity Framework does not keep JSON data Id

I am new to ASP.NET MVC, especially Entity Framework.
I am trying to save JSON data from external service to SQL server. I use Code First and it creates tables and inserts all records. But original ID values are changed to SQL auto generated ID value. For example, original ID 20050 become 1. Please check attached photos. I googled my problem and did not find a solution.
This is my fist time posting a question in stackoverflow too. Thanks in Advance.
you can set identity specification to false to that table usine DataAnnotations attribute.
[DatabaseGenerated(DatabaseGeneratedOption.None)];
public int ID { get; set; }
Than apply migration.

intellij idea, generating persistence mapping with relationships

i have my sql server database and i used "generate persistence mapping" in intellij to get my java classes, but the relationships defined on my sql server database were not mapped, it seems i can add them with the "add relationship" but i have a lot of tables with a lot of relationships so it is a pain in the ass to do it...
is there something that can map the database including the relationships? for example if i have a table user with a defined relationship of type one to many with a table potatoes, get a java class like this :
public class user{
private int iduser;
private String name;
private String password;
//relationship!!
private ArrayList<Potatoes>potatoes; //this is mapped like "private int id_potatoes" by intillij
public user(){}
//getters and setters
}
i´m using open JPA. amd i also can't find a way to auto generate DAO's with some basic methods (save, find, delete), i will have to code them myself!!! and im talking of atleast 40 tables!! it is ridiculous!!
i know there MUST be a way to do all this stuff automatically. oh, im using jsf 2, i dont know if that is relevant to this question anyway.
when you generate persistence mapping, just check the below box option
匚 Show default relationships
by this to generate , you will get the table relationships you want. Hope to help you :)

How to implement lookup table in mvc framework?

Asp.net MVC2: I have many dropdownlists in my mvc application. At first, I started by creating a table for each one with a unique ID and name and referring to them in the controllers and views. The application got bigger, and it was suggested that I use a lookup table, that contains lookuptype and lookupvalue as compound primary key and fill all the values for the dropdownlists in it. I've looked all over the internet, the only method used for mvc is one table for each dropdownlist! Can someone explain to me how I can implement it, and in detail please becoz I'm totally lost. A link to a tutorial would also be great.
I'm using vb.net and linq to sql.
Suppose your tables have columns ID, Name and Value. Now by having only one table that table would most probably look like this:
create table Lookup
(
LookupID int not null identity
primary key,
LookupTypeID int not null
references LookupType(LookupTypeID),
Name nvarchar(50) not null,
Value int not null,
unique(EnumTypeID, Name)
)
go
This table will make sure that within the same type names don't clash.
Anyway. You could of course have a similar application (not data) model class
public class EnumValue
{
public int Id { get; set; }
[Required]
public string Name { get; set; }
[Required]
public int Value { get; set; }
}
So whenever you get values of a certain type you can always generate an IList<EnumValue> of them and feed them to a particular drop down.
Since you didn't provide any valuable data this table and class could omit column/property Value, because depending on the inner workings it may not be needed at all. But you will know this best because you know your application requirements. Instead of this column/property only ID could do the trick.
I actually used a slightly different approach. I created a table that has a composite primary key, LookupName and LookupValue. Then in the datacontext I declared a method that takes the lookupname as a parameter, and then brings a list of lookupvalues whose lookupname match this parameter. In the original table (ex.Contact) I created a field called status, where the selected value will be saved. Then in the controller, I used viewdata to create dropdownlists.
Example: _db represents the datacontext
Viewdata('Status')= new selectlist(_db.Getlookupname('status'),'lookupname','lookupname')
and then in the view
html.dropdownlist('status')
I also named the dropdownlist with the same fieldname 'status' that is found in the target table 'Contact'.
And it worked, without any complexity or errors.
Thanks for the help. And I hope this will be helpful to someone else!
Here's a sort of tutorial on how to achieve a generic lookup service.
http://wtfperminute.blogspot.com/2011/02/working-with-reference-data-lookups.html
Similar to what you've done, but perhaps a bit more comprehensive.

3 linq-to-sql tables, all the same design, how to refactor them down to DRY?

So it appears with a simple designer change I can make the 3 tables implement the same interface without any additional coding, from here is it possible to get the rows to behave as queryable objects of the interface type without pulling the whole table down into memory and row by row cast?
each table can be treated as such:
[Table(Name="dbo.bLog")]
public partial class bLog : IBlogRow
where the interface is:
public interface IBlogRow
{
String App{get;set;}
String Version{get;set;}
String Message{get;set;}
DateTime dt{get;set;}
int? value {get;set;}
String ex {get;set;}
int? SecsTaken{get;set;}
bool? loanLevel{get;set;}
}
I think this is a contravariance or covariance problem. I can't seem to cast the table as a system.linq.table nor cast it as IQueryable ? How would I get this working so that i can write the same query code for all 3 tables that have the same design, and use them interchangeably?
Have you tried something along these lines?
IEnumerable<IBlogRow> GetRows()
{
DB db = new DB();
return db.BlogRows.Where(row => row.App == "something").Cast<IBlogRow>();
}
I think you'll have to cast after performing your query since LINQ to SQL won't be able to map IBlogRow properties to SQL table columns.
Update:
You could also try Dynamic Linq. You should be able to reuse most of the query code between the 3 tables. The drawback here is that you would give up strongly typed queries for magic strings.
Another option is using a PredicateBuilder. Take a look at the Generic Predicates example. This looks a lot like what you're describing.