Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent EFModelFileManager.ttinclude from deleting previously generated code by other model #57

Closed
Sancho-Lee opened this issue Dec 26, 2022 · 0 comments
Labels
released Issue is resolved in a current release
Milestone

Comments

@Sancho-Lee
Copy link

Sancho-Lee commented Dec 26, 2022

EFDesigner v4.2.1.3

Phenomenon:
If there are more than one model in a project, files created in other models are deleted when code is generated for a specific model.

In EFModelFileManager.ttinclude,

            private void ProjectSync(IEnumerable<string> keepFileNames)
            {
               Dictionary<ProjectItem, List<string>> current = GetCurrentState();

               string[] fileNames = keepFileNames as string[] ?? keepFileNames.ToArray();
               Dictionary<ProjectItem, List<string>> target = GetTargetState(fileNames);
               List<string> allTargetFiles = target.Keys.SelectMany(k => target[k]).ToList();

               List<string> existingFiles = new List<string>();

               foreach (ProjectItem parentItem in current.Keys.ToList())
               {
                  foreach (string filename in current[parentItem])
                  {
                     if (!allTargetFiles.Contains(filename) && !fileNames.Contains(filename)) // here
                        dte.Solution.FindProjectItem(filename)?.Delete();
                     else
                        existingFiles.Add(filename);
                  }
               }

               // just to be safe
               existingFiles = existingFiles.Distinct().ToList();

               foreach (ProjectItem parentItem in target.Keys.ToList())
               {
                  foreach (string filename in target[parentItem].Except(existingFiles).ToList())
                     parentItem.ProjectItems.AddFromFile(filename);
               }
            }

Suggestion:
I have solved this issue by deleting files if the specified file is in the directory of target files

I copied the EFModelFileManager.ttinclude file to model location and added a function to determine for deleting like below(code not optimized)

        bool shouldDelete(string[] targetFileNames, string fileName)
	{
				Dictionary<string, string> directories = new Dictionary<string, string>();
				foreach (string targetFilename in targetFileNames)
				{
					string? directory = Path.GetDirectoryName(targetFilename);
					if (directory != null)
					{
						if (!directories.ContainsKey(directory))
							directories.Add(directory, targetFilename);
					}
				}
				return directories.ContainsKey(Path.GetDirectoryName(fileName));
	}

and then,

            private void ProjectSync(IEnumerable<string> keepFileNames)
            {
               Dictionary<ProjectItem, List<string>> current = GetCurrentState();

               string[] fileNames = keepFileNames as string[] ?? keepFileNames.ToArray();
               Dictionary<ProjectItem, List<string>> target = GetTargetState(fileNames);
               List<string> allTargetFiles = target.Keys.SelectMany(k => target[k]).ToList();

               List<string> existingFiles = new List<string>();

               foreach (ProjectItem parentItem in current.Keys.ToList())
               {
                  foreach (string filename in current[parentItem])
                  {
                     if (!allTargetFiles.Contains(filename) && shouldDelete(fileNames, filename)) // changed
                        dte.Solution.FindProjectItem(filename)?.Delete();
                     else
                        existingFiles.Add(filename);
                  }
               }

               // just to be safe
               existingFiles = existingFiles.Distinct().ToList();

               foreach (ProjectItem parentItem in target.Keys.ToList())
               {
                  foreach (string filename in target[parentItem].Except(existingFiles).ToList())
                     parentItem.ProjectItems.AddFromFile(filename);
               }
            }

This worked for me.
Hope this helps.

@msawczyn msawczyn added the investigating Looking into this label Jan 4, 2023
@msawczyn msawczyn added this to the 4.2.3 milestone Mar 27, 2023
@msawczyn msawczyn added pending release Issue is resolved in the current codebase, will be published with the next release and removed investigating Looking into this labels Mar 27, 2023
@msawczyn msawczyn added released Issue is resolved in a current release and removed pending release Issue is resolved in the current codebase, will be published with the next release labels Mar 30, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
released Issue is resolved in a current release
Projects
None yet
Development

No branches or pull requests

2 participants