Ok I've seen this question Entity Framework: Generate Database From Model removes Stored Procedures from Model Store but I don't think this is solved. I have some function which I need to run at SQL Server in selection for filtering. I wasn't able to write that code in .NET so I just wrote that function in SQL Server as a function. I am using model-first, but I need that function at SQL level, and I use update model from database option to import the function. But when I make changes in the Entity Designer and generate database from model, the function mapping is erased. I understand that I shouldn't use model-first and db-first at the same time, but how can I solve my problem then? I've got this code that I can't write in C#, and I need it in my LINQ queries. It does work when I create an import and create the C# method with EdmFunction attribute which maps to the function at the database, but how can I make it persistent so the link between my model and my SQL function is preserved on update? Is writing the function that could translate in LINQ-to-Entities the only option?
Here is my SQL function
create function [dbo].[HammingDistance]
(@first bigint, @second bigint) returns int
as
begin
declare @xor bigint = @first ^ @second;
declare @one bigint = 1;
declare @diff int = 0;
declare @and bigint;
while (@xor != 0)
begin
set @and = @xor & @one;
if(@and = @one)
begin
set @diff = @diff + 1;
end
set @xor = @xor / 2;
end
return @diff;
end
(I'm not an SQL expert to this may not be the best way of doing this, so if there's a better way, correct me)
I need this to run in database WITHOUT importing anything to ASP.NET from the DB. If you have a way to translate this code into C# that would translate to SQL code in LINQ-to-Entities, it is also welcome.